Skip to content

Instantly share code, notes, and snippets.

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)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment