Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for a Base BSONError protocol #51

Open
Andrewangeta opened this issue Dec 16, 2018 · 3 comments
Open

Support for a Base BSONError protocol #51

Andrewangeta opened this issue Dec 16, 2018 · 3 comments

Comments

@Andrewangeta
Copy link
Collaborator

It will be useful to have a base protocol for BSONErrors that can be handled/caught at a global level for some applications. Currently if we want to catch a BSON error we would have to type cast to all the known types of errors. It would turn code that looks like this:

func handle(error: Error) {
    if let error = error as? BSONValueNotFound {
    ...
    } else if let error = error as? DeserializationError {
    ...
    } else if let error = error as? UnsupportedDocumentDecoding {
    ...
    }
    etc..
}

To code that would look like this:

if let error = error as? BSONError {
...
}
@Andrewangeta
Copy link
Collaborator Author

@Obbut any thoughts on this? 👀

@Obbut
Copy link
Collaborator

Obbut commented Feb 15, 2019

Hmm, currently all error types in BSON are private/internal, so it is impossible to catch them anyway.

We might implement a BSONError protocol, but another option could be to just convert the errors to an enum. The downside of using an enum is that people can switch over them which makes adding new cases a breaking change, but that can be solved like we did in MeowVapor.

The main upside of using an enum is that it allows writing catch BSONError.valueNotFound.

@Joannis
Copy link
Member

Joannis commented Feb 15, 2019

I suggested both solutions. The first one to @Andrewangeta and the second one to @Obbut . So far I prefer the second use case more since it allows us to cover more detail in the errors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants