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

Use optional initializers instead of throw #71

Closed
sebastianludwig opened this issue Jun 1, 2017 · 2 comments
Closed

Use optional initializers instead of throw #71

sebastianludwig opened this issue Jun 1, 2017 · 2 comments
Assignees

Comments

@sebastianludwig
Copy link
Contributor

During development I use a "Swift Error" breakpoint. Contentful throws unnecessary exceptions while working totally fine. This is confusing and bad style. For example ContentfulError throws instead of returning nil. Optional initializers should be used instead.

@loudmouth
Copy link
Contributor

loudmouth commented Jun 1, 2017

Hi @sebastianludwig , can you be a bit more explicit in your issue of which errors are being thrown and why this is problematic?

To say this is "bad style" is a subjective viewpoint, as the Swift error handling system is widely used for bubbling up errors through the system. ContentfulError instances are those returned by the API and these errors and their contained information including the message as well as the requestId, (which can be extremely helpful for communicating with Contentful's support team) should be available to SDK consumers so they can be handled appropriately.

Returning nil would mean all this information is lost and therefore clients of the SDK may not be able to reflect the error to the end-user properly.

@sebastianludwig
Copy link
Contributor Author

No no, real errors for error cases are just fine :-) I've got a "Swift Error Breakpoint" set in Xcode (added via the little + on the bottom). It's for example trigger in Client.swift:165.

            // Handle error thrown by CDA.
            if let error = try? ContentfulError(map: map) {
                completion(Result.error(error))
                return
            }

The case that the JSON does not represent an error (i.e. can't be mapped to a ContentfulError) isn't really an exception. It's actually more common than not. A more defensive (and I'd argue more "swifty") approach would be to use an optional/failing initializer (init?) that returns nil, if it's not an error.

However, on further investigation I found out, that the trows is dictated by ObjectMapper :-/ I've no spontaneous idea how to work around that.

It'd probably be best to use a different approach to check if it's an error or not than to try to create one. Currently it's also impossible to distinguish between "it's not an error" and "it's an error, but the parsing failed".

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

No branches or pull requests

2 participants