2 // OAuthSwiftError.swift
5 // Created by phimage on 02/10/16.
6 // Copyright © 2016 Dongri Jin. All rights reserved.
11 // MARK: - OAuthSwift errors
12 public enum OAuthSwiftError: Error {
14 /// Configuration problem with oauth provider.
15 case configurationError(message: String)
16 /// The provided token is expired, retrieve new token by using the refresh token
17 case tokenExpired(error: Error?)
18 /// State missing from request (you can set allowMissingStateCheck = true to ignore)
20 /// Returned state value is wrong
21 case stateNotEqual(state: String, responseState: String)
23 case serverError(message: String)
24 /// Failed to create URL \(urlString) not convertible to URL, please encode
25 case encodingError(urlString: String)
26 case authorizationPending
27 /// Failed to create request with \(urlString)
28 case requestCreation(message: String)
29 /// Authentification failed. No token
31 /// Please retain OAuthSwift object or handle
34 case requestError(error: Error, request: URLRequest)
38 public static let Domain = "OAuthSwiftError"
39 public static let ResponseDataKey = "OAuthSwiftError.response.data"
40 public static let ResponseKey = "OAuthSwiftError.response"
42 fileprivate enum Code: Int {
43 case configurationError = -1
44 case tokenExpired = -2
45 case missingState = -3
46 case stateNotEqual = -4
48 case encodingError = -6
49 case authorizationPending = -7
50 case requestCreation = -8
51 case missingToken = -9
53 case requestError = -11
57 fileprivate var code: Code {
59 case .configurationError: return Code.configurationError
60 case .tokenExpired: return Code.tokenExpired
61 case .missingState: return Code.missingState
62 case .stateNotEqual: return Code.stateNotEqual
63 case .serverError: return Code.serverError
64 case .encodingError: return Code.encodingError
65 case .authorizationPending: return Code.authorizationPending
66 case .requestCreation: return Code.requestCreation
67 case .missingToken: return Code.missingToken
68 case .retain: return Code.retain
69 case .requestError: return Code.requestError
70 case .cancelled : return Code.cancelled
74 public var underlyingError: Error? {
76 case .tokenExpired(let e): return e
77 case .requestError(let e, _): return e
82 public var underlyingMessage: String? {
84 case .serverError(let m): return m
85 case .configurationError(let m): return m
86 case .requestCreation(let m): return m
93 extension OAuthSwiftError: CustomStringConvertible {
95 public var description: String {
97 case .configurationError(let m): return "configurationError[\(m)]"
98 case .tokenExpired(let e): return "tokenExpired[\(String(describing: e))]"
99 case .missingState: return "missingState"
100 case .stateNotEqual(let s, let e): return "stateNotEqual[\(s)<>\(e)]"
101 case .serverError(let m): return "serverError[\(m)]"
102 case .encodingError(let urlString): return "encodingError[\(urlString)]"
103 case .authorizationPending: return "authorizationPending"
104 case .requestCreation(let m): return "requestCreation[\(m)]"
105 case .missingToken: return "missingToken"
106 case .retain: return "retain"
107 case .requestError(let e, _): return "requestError[\(e)]"
108 case .cancelled : return "cancelled"
113 extension OAuthSwift {
115 static func retainError(_ failureHandler: FailureHandler?) {
116 #if !OAUTH_NO_RETAIN_ERROR
117 failureHandler?(OAuthSwiftError.retain)
124 extension OAuthSwiftError: CustomNSError {
126 public static var errorDomain: String { return OAuthSwiftError.Domain }
128 public var errorCode: Int { return self.code.rawValue }
130 /// The user-info dictionary.
131 public var errorUserInfo: [String: Any] {
133 case .configurationError(let m): return ["message": m]
134 case .serverError(let m): return ["message": m]
135 case .requestCreation(let m): return ["message": m]
137 case .tokenExpired(let e): return ["error": e as Any]
138 case .requestError(let e, let request): return ["error": e, "request": request]
140 case .encodingError(let urlString): return ["url": urlString]
142 case .stateNotEqual(let s, let e): return ["state": s, "expected": e]
147 public var _code: Int {
148 return self.code.rawValue
150 public var _domain: String {
151 return OAuthSwiftError.Domain