Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Alamofire Request Error Handling - From their documentation
Alamofire.request(urlString).responseJSON { response in
guard case let .failure(error) = response.result else { return }
if let error = error as? AFError {
switch error {
case .invalidURL(let url):
print("Invalid URL: \(url) - \(error.localizedDescription)")
case .parameterEncodingFailed(let reason):
print("Parameter encoding failed: \(error.localizedDescription)")
print("Failure Reason: \(reason)")
case .multipartEncodingFailed(let reason):
print("Multipart encoding failed: \(error.localizedDescription)")
print("Failure Reason: \(reason)")
case .responseValidationFailed(let reason):
print("Response validation failed: \(error.localizedDescription)")
print("Failure Reason: \(reason)")
switch reason {
case .dataFileNil, .dataFileReadFailed:
print("Downloaded file could not be read")
case .missingContentType(let acceptableContentTypes):
print("Content Type Missing: \(acceptableContentTypes)")
case .unacceptableContentType(let acceptableContentTypes, let responseContentType):
print("Response content type: \(responseContentType) was unacceptable: \(acceptableContentTypes)")
case .unacceptableStatusCode(let code):
print("Response status code was unacceptable: \(code)")
}
case .responseSerializationFailed(let reason):
print("Response serialization failed: \(error.localizedDescription)")
print("Failure Reason: \(reason)")
}
print("Underlying error: \(error.underlyingError)")
} else if let error = error as? URLError {
print("URLError occurred: \(error)")
} else {
print("Unknown error: \(error)")
}
}
@lexuanquynh
Copy link

Thanks for your post

@Coder-ACJHP
Copy link

Alamofire 5 & Swift 5

       `if let error = err as? AFError {     
            switch error {
            case .createUploadableFailed(let error):
                debugPrint("Create Uploadable Failed, description: \(error.localizedDescription)")
            case .createURLRequestFailed(let error):
                debugPrint("Create URL Request Failed, description: \(error.localizedDescription)")
            case .downloadedFileMoveFailed(let error, let source, let destination):
                debugPrint("Downloaded File Move Failed, description: \(error.localizedDescription)")
                debugPrint("Source: \(source), Destination: \(destination)")
            case .explicitlyCancelled:
                debugPrint("Explicitly Cancelled - \(error.localizedDescription)")
            case .invalidURL(let url):
                debugPrint("Invalid URL: \(url) - \(error.localizedDescription)")
            case .multipartEncodingFailed(let reason):
                debugPrint("Multipart encoding failed, description: \(error.localizedDescription)")
                debugPrint("Failure Reason: \(reason)")
            case .parameterEncodingFailed(let reason):
                debugPrint("Parameter encoding failed, description: \(error.localizedDescription)")
                debugPrint("Failure Reason: \(reason)")
            case .parameterEncoderFailed(let reason):
                debugPrint("Parameter Encoder Failed, description: \(error.localizedDescription)")
                debugPrint("Failure Reason: \(reason)")
            case .requestAdaptationFailed(let error):
                debugPrint("Request Adaptation Failed, description: \(error.localizedDescription)")
            case .requestRetryFailed(let retryError, let originalError):
                debugPrint("Request Retry Failed")
                debugPrint("Original error description: \(originalError.localizedDescription)")
                debugPrint("Retry error description: \(retryError.localizedDescription)")
            case .responseValidationFailed(let reason):
                debugPrint("Response validation failed, description: \(error.localizedDescription)")
                switch reason {
                case .dataFileNil, .dataFileReadFailed:
                    debugPrint("Downloaded file could not be read")
                case .missingContentType(let acceptableContentTypes):
                    debugPrint("Content Type Missing: \(acceptableContentTypes)")
                case .unacceptableContentType(let acceptableContentTypes, let responseContentType):
                    debugPrint("Response content type: \(responseContentType) was unacceptable: \(acceptableContentTypes)")
                case .unacceptableStatusCode(let code):
                    debugPrint("Response status code was unacceptable: \(code)")
                default: break
                }
            case .responseSerializationFailed(let reason):
                debugPrint("Response serialization failed: \(error.localizedDescription)")
                debugPrint("Failure Reason: \(reason)")
            case .serverTrustEvaluationFailed(let reason):
                debugPrint("Server Trust Evaluation Failed, description: \(error.localizedDescription)")
                debugPrint("Failure Reason: \(reason)")
            case .sessionDeinitialized:
                debugPrint("Session Deinitialized, description: \(error.localizedDescription)")
            case .sessionInvalidated(let error):
                debugPrint("Session Invalidated, description: \(error?.localizedDescription ?? "")")
            case .sessionTaskFailed(let error):
                debugPrint("Session Task Failed, description: \(error.localizedDescription)")
            case .urlRequestValidationFailed(let reason):
                debugPrint("Url Request Validation Failed, description: \(error.localizedDescription)")
                debugPrint("Failure Reason: \(reason)")
            }
        }`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment