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

How should I continue after handleErrorAsync inside a custom ApolloErrorInterceptor? #3368

Closed
avielg opened this issue Apr 16, 2024 · 5 comments · Fixed by apollographql/apollo-ios-dev#336
Assignees
Labels
question Issues that have a question which should be addressed

Comments

@avielg
Copy link

avielg commented Apr 16, 2024

Question

I have an error interceptor, meant to log errors and notify users of my SDK via a delegate.

After I am done with the error inside handleErrorAsync, what should I do? Should I:

  1. Call proceedAsync(request:response:interceptor:completion:)? (Assume no because I can't pass self as the interceptor, and the chain is done AFAIU?)
  2. Call chain.handleErrorAsync(request:response:completion:) to pass the error to any other interceptor? Or will that just cause infinite loop?
  3. Do nothing?

Thank you! 🙏

@avielg avielg added the question Issues that have a question which should be addressed label Apr 16, 2024
@calvincestari
Copy link
Member

Hi @avielg - when you receive an error like that it should be considered a terminal error re. the request so no you won't be able to continue from where the error was generated. You'd have to restart the request again.

Copy link
Contributor

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo iOS usage and allow us to serve you better.

@avielg
Copy link
Author

avielg commented Apr 17, 2024

@calvincestari Yeap I think my confusion was due to thinking that the error interceptor is an array like the regular interceptors - thinking that there might be other error interceptor that should also "get their turn" to do something with the error, but since there can only be one - I just need to pass the request to the original caller by calling the completion and passing .failure(error) - which wasn't initially clear to me.

I wonder if it makes sense to have a small example showing this in the docs? Specifically where there are examples to the regular interceptors (https://www.apollographql.com/docs/ios/networking/request-pipeline/#example-interceptors), and the error interceptor is mentioned there, but without an example on usage?

Something like this perhaps?

class LoggerErrorInterceptor: ApolloErrorInterceptor {
    weak var errorLogger: MyErrorLogger?

    init(errorLogger: MyErrorLogger) {
        self.errorLogger = errorLogger
    }

    func handleErrorAsync<Operation: GraphQLOperation>(
        error: Error,
        chain: RequestChain,
        request: Apollo.HTTPRequest<Operation>,
        response: Apollo.HTTPResponse<Operation>?,
        completion: @escaping (Result<GraphQLResult<Operation.Data>, Error>) -> Void
    ) {
        errorLogger?.requestFailed(response?.httpResponse, withError: error)
        completion(.failure(error))
    }
}

@calvincestari
Copy link
Member

Sure thing @avielg. I've got a PR up at apollographql/apollo-ios-dev#336 to add that, thanks for the suggestion.

@avielg
Copy link
Author

avielg commented Apr 18, 2024

Amazing thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issues that have a question which should be addressed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants