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

Better Handling of HTTPResponseError with json + Tests #756

Merged
merged 2 commits into from
Sep 10, 2019

Conversation

designatednerd
Copy link
Contributor

This PR addresses #742 (which is a re-file of #477), and makes sure that when an HTTPResponseError is received, errors are properly parsed.

I also added some tests on this as a net for the future.

@designatednerd designatednerd added this to the 0.16.0 milestone Sep 9, 2019
@designatednerd
Copy link
Contributor Author

@jordanhbuiltbyhq Mind taking a look at this to make sure this is the behavior you were looking for?

@jordanhbuiltbyhq
Copy link

This is lovely, thanks! Any expectation for when this would go live?

@designatednerd
Copy link
Contributor Author

I'll take a look and see if anything else needs to be patched but otherwise within the next day or two

@jordanhbuiltbyhq
Copy link

jordanhbuiltbyhq commented Sep 10, 2019

Oh I wanted to check, is there a better way to detect an unauthenticated error? Currently I have this not-so-pretty chunk of code:

        if let graphQLError = error as? GraphQLHTTPResponseError {
            if let bodyData = graphQLError.body {
                if let jsonObject = try? JSONSerialization.jsonObject(with: bodyData, options: []) as? [String: Any] {
                    if let errors = jsonObject["errors"] as? [[String: Any]] {
                        if let extensions = errors.first?["extensions"] as? [String: Any] {
                            if let code = extensions["code"] as? String {
                                if code == "UNAUTHENTICATED" {
                                    //TODO: Go to login screen
                                }
                            }
                        }
                    }
                }
            }
        }

@designatednerd
Copy link
Contributor Author

Woo, pyramid of doom! Takes me back to the early days of Swift 😃

So a couple suggestions: First, use guard let in order to stack your unwraps instead of pyramiding them (you'll see what I mean in the example below). Once this feature lands, you should be able to do something like:

guard 
    let responseError = error as? GraphQLHTTPResponseError,
    let graphQLError = responseError.graphQLErrors?.first, 
    let extensions = graphQLError.extensions,
    let code = extensions["code"] else {
       // No error code to deal with 
      return
}

switch (code) {
case "UNAUTHENTICATED": 
    // Kick to login screen
default: 
   // Whatever else you need to do
}

@designatednerd designatednerd modified the milestones: 0.16.0, 0.15.2 Sep 10, 2019
@designatednerd
Copy link
Contributor Author

OK gonna merge this, it'll go out in the next patch release - I'll ping your other issue once it ships.

@designatednerd designatednerd merged commit ea10e2a into master Sep 10, 2019
@designatednerd designatednerd deleted the enhance/errorz branch September 10, 2019 19:50
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

Successfully merging this pull request may close these issues.

2 participants