Moya: Not getting valid error code for network failure

Created on 19 Jun 2020  路  7Comments  路  Source: Moya/Moya

I am handling the MoyaError in my request function using the following switch case -

switch moyaError {
   // All the different kinds of errors handled
  case .underlying(let underlyingError as NSError, _):
            print("code", underlyingError.code,  underlyingError.localizedDescription)
}

And I am getting the following output in case of network failure -

```
code 13 URLSessionTask failed with error: The Internet connection appears to be offline.
````

Shouldn't I be getting -1009 as the code for NSURLErrorNotConnectedToInternet instead of 13? Or am I missing something here.

My Moya version is 14.0.0

stale

Most helpful comment

It's not a solution. But I'm currently using this workaround:

            if let alamofireError = moyaError.errorUserInfo["NSUnderlyingError"] as? Alamofire.AFError,
              let underlyingError = alamofireError.underlyingError as NSError?,
              [NSURLErrorNotConnectedToInternet, NSURLErrorDataNotAllowed].contains(underlyingError.code) {

                return ConnectionError.noInternetConnection

            } 

It's a very verbose and ugly code, so I hope some solution be provided anytime soon.

All 7 comments

This issue has been marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

This issue has been auto-closed because there hasn't been any activity for at least 21 days. However, we really appreciate your contribution, so thank you for that! 馃檹 Also, feel free to open a new issue if you still experience this problem 馃憤.

dizzy

I have the same problem. receiving error code 13 when I should be receiving a -1009.

I would like to ask if there is a final solution? How to solve it?
thank you!

I have the same problem. receiving error code 13 when I should be receiving a -1009.

I would like to ask if there is a final solution? How to solve it?
thank you!

It's not a solution. But I'm currently using this workaround:

            if let alamofireError = moyaError.errorUserInfo["NSUnderlyingError"] as? Alamofire.AFError,
              let underlyingError = alamofireError.underlyingError as NSError?,
              [NSURLErrorNotConnectedToInternet, NSURLErrorDataNotAllowed].contains(underlyingError.code) {

                return ConnectionError.noInternetConnection

            } 

It's a very verbose and ugly code, so I hope some solution be provided anytime soon.

Was this page helpful?
0 / 5 - 0 ratings