Forked from perlguy99/Alamofire.request.error.handling.swift
Created
March 29, 2021 10:52
-
-
Save iosdroid/b5b40f8cd6da749e028c5ed531ae3f36 to your computer and use it in GitHub Desktop.
Alamofire Request Error Handling - From their documentation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment