Alamofire: [Alamofire 5] RequestInterceptor retry not called on error

Created on 19 Mar 2019  路  2Comments  路  Source: Alamofire/Alamofire

[ ] I've read, understood, and done my best to follow the CONTRIBUTING guidelines.

What did you do?

Created a RequestInterceptor
session.request(route, interceptor: EnvironmentInterceptor())

struct EnvironmentInterceptor: RequestInterceptor {

    func adapt(_ urlRequest: URLRequest, for session: Session, completion: @escaping (Result<URLRequest>) -> Void) {
        var adaptedRequest = urlRequest
        guard let token = AtraqService.shared.user?.token.accessToken else {
            completion(.success(adaptedRequest))
            return
        }
        adaptedRequest.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
        completion(.success(adaptedRequest))
    }
    // Not called on 401
    func retry(_ request: Request, for session: Session, dueTo error: Error, completion: @escaping (RetryResult) -> Void) {
        if let response = request.task?.response as? HTTPURLResponse, response.statusCode == 401{

    }
}

What did you expect to happen?

retry() to be called on 401 error

What happened instead?

adapt was called first...
retry was not called on error

Alamofire Environment

5.0.0beta3
10.1
4.2
12.1.4
10.14.3

Demo Project

none

support

Most helpful comment

Retry is only triggered when an error is produced. By default, there's no error produced by a 401 response, by URLSession or Alamofire. If you want one, you'll need add a validate() call, either using the default setting or customized with the behavior you want.

All 2 comments

Retry is only triggered when an error is produced. By default, there's no error produced by a 401 response, by URLSession or Alamofire. If you want one, you'll need add a validate() call, either using the default setting or customized with the behavior you want.

@jshier Yes that works.

Was this page helpful?
0 / 5 - 0 ratings