3

Strange thing happened to me while implementing networking based on Alamofire. So, my backend can return me 409 and when I get it I want to react properly, so I went for a regular solution:

Alamofire.request(someRequest).responseJSON { response in
    switch response.result {
    case .Success(let value):
        // I get 409 here...
        if response.response?.statusCode == 409 {
            // ...
        }
    case .Failure(let error):
        // While it should be here...
    }
}

Why is it happening? Is it some sort of backend issue or what? I can only thing of one explanation: Request completed, so it's .Success even though you got 409, but this explanation doesn't convince me

cojoj
  • 6,405
  • 4
  • 30
  • 52

1 Answers1

3

HTTP 4.xx class of status codes are intended for situations in which the client seems to have erred, so in normal cases you are to show some kind of error message to the user when it was generated by the server.

In Alamofire world, you can tap into .validate() method if you want response to generate an error if it had an unacceptable status code:

Alamofire.request(someRequest)
  .validate(statusCode: 200..<300)
  .responseJSON { response in
    switch response.result {
    case .Success(let value):
      ...
    case .Failure(let error):
      // You'll handle HTTP 409 error here...
  }
}
Ozgur Vatansever
  • 49,246
  • 17
  • 84
  • 119