// // AFError.swift // // Copyright (c) 2014-2018 Alamofire Software Foundation (http://alamofire.org/) // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // import Foundation /// `AFError` is the error type returned by Alamofire. It encompasses a few different types of errors, each with /// their own associated reasons. /// /// - explicitlyCancelled: Returned when a `Request` is explicitly cancelled. /// - invalidURL: Returned when a `URLConvertible` type fails to create a valid `URL`. /// - parameterEncodingFailed: Returned when a parameter encoding object throws an error during the encoding process. /// - multipartEncodingFailed: Returned when some step in the multipart encoding process fails. /// - responseValidationFailed: Returned when a `validate()` call fails. /// - responseSerializationFailed: Returned when a response serializer encounters an error in the serialization process. /// - certificatePinningFailed: Returned when a response fails certificate pinning. public enum AFError: Error { /// The underlying reason the parameter encoding error occurred. /// /// - missingURL: The URL request did not have a URL to encode. /// - jsonEncodingFailed: JSON serialization failed with an underlying system error during the /// encoding process. public enum ParameterEncodingFailureReason { case missingURL case jsonEncodingFailed(error: Error) } /// Underlying reason the parameter encoder error occured. public enum ParameterEncoderFailureReason { /// Possible missing components. public enum RequiredComponent { /// The `URL` was missing or unable to be extracted from the passed `URLRequest` or during encoding. case url /// The `HTTPMethod` could not be extracted from the passed `URLRequest`. case httpMethod(rawValue: String) } /// A `RequiredComponent` was missing during encoding. case missingRequiredComponent(RequiredComponent) /// The underlying encoder failed with the associated error. case encoderFailed(error: Error) } /// The underlying reason the multipart encoding error occurred. /// /// - bodyPartURLInvalid: The `fileURL` provided for reading an encodable body part isn't a /// file URL. /// - bodyPartFilenameInvalid: The filename of the `fileURL` provided has either an empty /// `lastPathComponent` or `pathExtension. /// - bodyPartFileNotReachable: The file at the `fileURL` provided was not reachable. /// - bodyPartFileNotReachableWithError: Attempting to check the reachability of the `fileURL` provided threw /// an error. /// - bodyPartFileIsDirectory: The file at the `fileURL` provided is actually a directory. /// - bodyPartFileSizeNotAvailable: The size of the file at the `fileURL` provided was not returned by /// the system. /// - bodyPartFileSizeQueryFailedWithError: The attempt to find the size of the file at the `fileURL` provided /// threw an error. /// - bodyPartInputStreamCreationFailed: An `InputStream` could not be created for the provided `fileURL`. /// - outputStreamCreationFailed: An `OutputStream` could not be created when attempting to write the /// encoded data to disk. /// - outputStreamFileAlreadyExists: The encoded body data could not be writtent disk because a file /// already exists at the provided `fileURL`. /// - outputStreamURLInvalid: The `fileURL` provided for writing the encoded body data to disk is /// not a file URL. /// - outputStreamWriteFailed: The attempt to write the encoded body data to disk failed with an /// underlying error. /// - inputStreamReadFailed: The attempt to read an encoded body part `InputStream` failed with /// underlying system error. public enum MultipartEncodingFailureReason { case bodyPartURLInvalid(url: URL) case bodyPartFilenameInvalid(in: URL) case bodyPartFileNotReachable(at: URL) case bodyPartFileNotReachableWithError(atURL: URL, error: Error) case bodyPartFileIsDirectory(at: URL) case bodyPartFileSizeNotAvailable(at: URL) case bodyPartFileSizeQueryFailedWithError(forURL: URL, error: Error) case bodyPartInputStreamCreationFailed(for: URL) case outputStreamCreationFailed(for: URL) case outputStreamFileAlreadyExists(at: URL) case outputStreamURLInvalid(url: URL) case outputStreamWriteFailed(error: Error) case inputStreamReadFailed(error: Error) } /// The underlying reason the response validation error occurred. /// /// - dataFileNil: The data file containing the server response did not exist. /// - dataFileReadFailed: The data file containing the server response could not be read. /// - missingContentType: The response did not contain a `Content-Type` and the `acceptableContentTypes` /// provided did not contain wildcard type. /// - unacceptableContentType: The response `Content-Type` did not match any type in the provided /// `acceptableContentTypes`. /// - unacceptableStatusCode: The response status code was not acceptable. public enum ResponseValidationFailureReason { case dataFileNil case dataFileReadFailed(at: URL) case missingContentType(acceptableContentTypes: [String]) case unacceptableContentType(acceptableContentTypes: [String], responseContentType: String) case unacceptableStatusCode(code: Int) } /// The underlying reason the response serialization error occurred. /// /// - inputDataNil: The server response contained no data. /// - inputDataNilOrZeroLength: The server response contained no data or the data was zero length. /// - inputFileNil: The file containing the server response did not exist. /// - inputFileReadFailed: The file containing the server response could not be read. /// - stringSerializationFailed: String serialization failed using the provided `String.Encoding`. /// - jsonSerializationFailed: JSON serialization failed with an underlying system error. /// - invalidEmptyResponse: Generic serialization failed for an empty response that wasn't type `Empty`. public enum ResponseSerializationFailureReason { case inputDataNilOrZeroLength case inputFileNil case inputFileReadFailed(at: URL) case stringSerializationFailed(encoding: String.Encoding) case jsonSerializationFailed(error: Error) case invalidEmptyResponse(type: String) case jsonDecodingFailed(error: Error) } /// Underlying reason a server trust evaluation error occured. public enum ServerTrustFailureReason { /// The output of a server trust evaluation. public struct Output { /// The host for which the evaluation was performed. public let host: String /// The `SecTrust` value which was evaluated. public let trust: SecTrust /// The `OSStatus` of evaluation operation. public let status: OSStatus /// The result of the evaluation operation. public let result: SecTrustResultType /// Creates an `Output` value from the provided values. init(_ host: String, _ trust: SecTrust, _ status: OSStatus, _ result: SecTrustResultType) { self.host = host self.trust = trust self.status = status self.result = result } } case noRequiredEvaluator(host: String) /// No certificates were found with which to perform the trust evaluation. case noCertificatesFound /// No public keys were found with which to perform the trust evaluation. case noPublicKeysFound /// During evaluation, application of the associated `SecPolicy` failed. case policyApplicationFailed(trust: SecTrust, policy: SecPolicy, status: OSStatus) /// During evaluation, setting the associated anchor certificates failed. case settingAnchorCertificatesFailed(status: OSStatus, certificates: [SecCertificate]) /// During evaluation, creation of the revocation policy failed. case revocationPolicyCreationFailed /// Default evaluation failed with the associated `Output`. case defaultEvaluationFailed(output: Output) /// Host validation failed with the associated `Output`. case hostValidationFailed(output: Output) /// Revocation check failed with the associated `Output` and options. case revocationCheckFailed(output: Output, options: RevocationTrustEvaluator.Options) /// Certificate pinning failed. case certificatePinningFailed(host: String, trust: SecTrust, pinnedCertificates: [SecCertificate], serverCertificates: [SecCertificate]) /// Public key pinning failed. case publicKeyPinningFailed(host: String, trust: SecTrust, pinnedKeys: [SecKey], serverKeys: [SecKey]) } case explicitlyCancelled case invalidURL(url: URLConvertible) case parameterEncodingFailed(reason: ParameterEncodingFailureReason) case parameterEncoderFailed(reason: ParameterEncoderFailureReason) case multipartEncodingFailed(reason: MultipartEncodingFailureReason) case responseValidationFailed(reason: ResponseValidationFailureReason) case responseSerializationFailed(reason: ResponseSerializationFailureReason) case serverTrustEvaluationFailed(reason: ServerTrustFailureReason) } extension Error { /// Returns the instance cast as an `AFError`. public var asAFError: AFError? { return self as? AFError } } // MARK: - Error Booleans extension AFError { /// Returns whether the `AFError` is an explicitly cancelled error. public var isExplicitlyCancelledError: Bool { if case .explicitlyCancelled = self { return true } return false } /// Returns whether the AFError is an invalid URL error. public var isInvalidURLError: Bool { if case .invalidURL = self { return true } return false } /// Returns whether the AFError is a parameter encoding error. When `true`, the `underlyingError` property will /// contain the associated value. public var isParameterEncodingError: Bool { if case .parameterEncodingFailed = self { return true } return false } /// Returns whether the instance is a parameter encoder error. public var isParameterEncoderError: Bool { if case .parameterEncoderFailed = self { return true } return false } /// Returns whether the AFError is a multipart encoding error. When `true`, the `url` and `underlyingError` properties /// will contain the associated values. public var isMultipartEncodingError: Bool { if case .multipartEncodingFailed = self { return true } return false } /// Returns whether the `AFError` is a response validation error. When `true`, the `acceptableContentTypes`, /// `responseContentType`, and `responseCode` properties will contain the associated values. public var isResponseValidationError: Bool { if case .responseValidationFailed = self { return true } return false } /// Returns whether the `AFError` is a response serialization error. When `true`, the `failedStringEncoding` and /// `underlyingError` properties will contain the associated values. public var isResponseSerializationError: Bool { if case .responseSerializationFailed = self { return true } return false } /// Returns whether the `AFError` is a server trust evaluation error. public var isServerTrustEvaluationError: Bool { if case .serverTrustEvaluationFailed = self { return true } return false } } // MARK: - Convenience Properties extension AFError { /// The `URLConvertible` associated with the error. public var urlConvertible: URLConvertible? { switch self { case .invalidURL(let url): return url default: return nil } } /// The `URL` associated with the error. public var url: URL? { switch self { case .multipartEncodingFailed(let reason): return reason.url default: return nil } } /// The `Error` returned by a system framework associated with a `.parameterEncodingFailed`, /// `.parameterEncoderFailed`, `.multipartEncodingFailed` or `.responseSerializationFailed` error. public var underlyingError: Error? { switch self { case .parameterEncodingFailed(let reason): return reason.underlyingError case .parameterEncoderFailed(let reason): return reason.underlyingError case .multipartEncodingFailed(let reason): return reason.underlyingError case .responseSerializationFailed(let reason): return reason.underlyingError default: return nil } } /// The acceptable `Content-Type`s of a `.responseValidationFailed` error. public var acceptableContentTypes: [String]? { switch self { case .responseValidationFailed(let reason): return reason.acceptableContentTypes default: return nil } } /// The response `Content-Type` of a `.responseValidationFailed` error. public var responseContentType: String? { switch self { case .responseValidationFailed(let reason): return reason.responseContentType default: return nil } } /// The response code of a `.responseValidationFailed` error. public var responseCode: Int? { switch self { case .responseValidationFailed(let reason): return reason.responseCode default: return nil } } /// The `String.Encoding` associated with a failed `.stringResponse()` call. public var failedStringEncoding: String.Encoding? { switch self { case .responseSerializationFailed(let reason): return reason.failedStringEncoding default: return nil } } } extension AFError.ParameterEncodingFailureReason { var underlyingError: Error? { switch self { case .jsonEncodingFailed(let error): return error default: return nil } } } extension AFError.ParameterEncoderFailureReason { var underlyingError: Error? { switch self { case .encoderFailed(let error): return error default: return nil } } } extension AFError.MultipartEncodingFailureReason { var url: URL? { switch self { case .bodyPartURLInvalid(let url), .bodyPartFilenameInvalid(let url), .bodyPartFileNotReachable(let url), .bodyPartFileIsDirectory(let url), .bodyPartFileSizeNotAvailable(let url), .bodyPartInputStreamCreationFailed(let url), .outputStreamCreationFailed(let url), .outputStreamFileAlreadyExists(let url), .outputStreamURLInvalid(let url), .bodyPartFileNotReachableWithError(let url, _), .bodyPartFileSizeQueryFailedWithError(let url, _): return url default: return nil } } var underlyingError: Error? { switch self { case .bodyPartFileNotReachableWithError(_, let error), .bodyPartFileSizeQueryFailedWithError(_, let error), .outputStreamWriteFailed(let error), .inputStreamReadFailed(let error): return error default: return nil } } } extension AFError.ResponseValidationFailureReason { var acceptableContentTypes: [String]? { switch self { case .missingContentType(let types), .unacceptableContentType(let types, _): return types default: return nil } } var responseContentType: String? { switch self { case .unacceptableContentType(_, let responseType): return responseType default: return nil } } var responseCode: Int? { switch self { case .unacceptableStatusCode(let code): return code default: return nil } } } extension AFError.ResponseSerializationFailureReason { var failedStringEncoding: String.Encoding? { switch self { case .stringSerializationFailed(let encoding): return encoding default: return nil } } var underlyingError: Error? { switch self { case .jsonSerializationFailed(let error): return error default: return nil } } } extension AFError.ServerTrustFailureReason { var output: AFError.ServerTrustFailureReason.Output? { switch self { case let .defaultEvaluationFailed(output), let .hostValidationFailed(output), let .revocationCheckFailed(output, _): return output default: return nil } } } // MARK: - Error Descriptions extension AFError: LocalizedError { public var errorDescription: String? { switch self { case .explicitlyCancelled: return "Request explicitly cancelled." case .invalidURL(let url): return "URL is not valid: \(url)" case .parameterEncodingFailed(let reason): return reason.localizedDescription case .parameterEncoderFailed(let reason): return reason.localizedDescription case .multipartEncodingFailed(let reason): return reason.localizedDescription case .responseValidationFailed(let reason): return reason.localizedDescription case .responseSerializationFailed(let reason): return reason.localizedDescription case .serverTrustEvaluationFailed: return "Server trust evaluation failed." } } } extension AFError.ParameterEncodingFailureReason { var localizedDescription: String { switch self { case .missingURL: return "URL request to encode was missing a URL" case .jsonEncodingFailed(let error): return "JSON could not be encoded because of error:\n\(error.localizedDescription)" } } } extension AFError.ParameterEncoderFailureReason { var localizedDescription: String { switch self { case .missingRequiredComponent(let component): return "Encoding failed due to a missing request component: \(component)" case .encoderFailed(let error): return "The underlying encoder failed with the error: \(error)" } } } extension AFError.MultipartEncodingFailureReason { var localizedDescription: String { switch self { case .bodyPartURLInvalid(let url): return "The URL provided is not a file URL: \(url)" case .bodyPartFilenameInvalid(let url): return "The URL provided does not have a valid filename: \(url)" case .bodyPartFileNotReachable(let url): return "The URL provided is not reachable: \(url)" case .bodyPartFileNotReachableWithError(let url, let error): return ( "The system returned an error while checking the provided URL for " + "reachability.\nURL: \(url)\nError: \(error)" ) case .bodyPartFileIsDirectory(let url): return "The URL provided is a directory: \(url)" case .bodyPartFileSizeNotAvailable(let url): return "Could not fetch the file size from the provided URL: \(url)" case .bodyPartFileSizeQueryFailedWithError(let url, let error): return ( "The system returned an error while attempting to fetch the file size from the " + "provided URL.\nURL: \(url)\nError: \(error)" ) case .bodyPartInputStreamCreationFailed(let url): return "Failed to create an InputStream for the provided URL: \(url)" case .outputStreamCreationFailed(let url): return "Failed to create an OutputStream for URL: \(url)" case .outputStreamFileAlreadyExists(let url): return "A file already exists at the provided URL: \(url)" case .outputStreamURLInvalid(let url): return "The provided OutputStream URL is invalid: \(url)" case .outputStreamWriteFailed(let error): return "OutputStream write failed with error: \(error)" case .inputStreamReadFailed(let error): return "InputStream read failed with error: \(error)" } } } extension AFError.ResponseSerializationFailureReason { var localizedDescription: String { switch self { case .inputDataNilOrZeroLength: return "Response could not be serialized, input data was nil or zero length." case .inputFileNil: return "Response could not be serialized, input file was nil." case .inputFileReadFailed(let url): return "Response could not be serialized, input file could not be read: \(url)." case .stringSerializationFailed(let encoding): return "String could not be serialized with encoding: \(encoding)." case .jsonSerializationFailed(let error): return "JSON could not be serialized because of error:\n\(error.localizedDescription)" case .invalidEmptyResponse(let type): return "Empty response could not be serialized to type: \(type). Use Empty as the expected type for such responses." case .jsonDecodingFailed(let error): return "JSON could not be decoded because of error:\n\(error.localizedDescription)" } } } extension AFError.ResponseValidationFailureReason { var localizedDescription: String { switch self { case .dataFileNil: return "Response could not be validated, data file was nil." case .dataFileReadFailed(let url): return "Response could not be validated, data file could not be read: \(url)." case .missingContentType(let types): return ( "Response Content-Type was missing and acceptable content types " + "(\(types.joined(separator: ","))) do not match \"*/*\"." ) case .unacceptableContentType(let acceptableTypes, let responseType): return ( "Response Content-Type \"\(responseType)\" does not match any acceptable types: " + "\(acceptableTypes.joined(separator: ","))." ) case .unacceptableStatusCode(let code): return "Response status code was unacceptable: \(code)." } } } extension AFError.ServerTrustFailureReason { var localizedDescription: String { switch self { case let .noRequiredEvaluator(host): return "A ServerTrustEvaluating value is required for host \(host) but none was found." case .noCertificatesFound: return "No certificates were found or provided for evaluation." case .noPublicKeysFound: return "No public keys were found or provided for evaluation." case .policyApplicationFailed: return "Attempting to set a SecPolicy failed." case .settingAnchorCertificatesFailed: return "Attempting to set the provided certificates as anchor certificates failed." case .revocationPolicyCreationFailed: return "Attempting to create a revocation policy failed." case let .defaultEvaluationFailed(output): return "Default evaluation failed for host \(output.host)." case let .hostValidationFailed(output): return "Host validation failed for host \(output.host)." case let .revocationCheckFailed(output, _): return "Revocation check failed for host \(output.host)." case let .certificatePinningFailed(host, _, _, _): return "Certificate pinning failed for host \(host)." case let .publicKeyPinningFailed(host, _, _, _): return "Public key pinning failed for host \(host)." } } }