• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hyperwallet / hyperwallet-ios-sdk / 15826401626

21 May 2025 02:36PM UTC coverage: 96.705%. Remained the same
15826401626

push

github

web-flow
Initial commit for CodeQL (#152)

* Initial commit for CodeQL

* Updating language

1937 of 2003 relevant lines covered (96.7%)

18.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/Sources/HyperwalletError.swift
1
//
2
// Copyright 2018 - Present Hyperwallet
3
//
4
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
5
// and associated documentation files (the "Software"), to deal in the Software without restriction,
6
// including without limitation the rights to use, copy, modify, merge, publish, distribute,
7
// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8
// furnished to do so, subject to the following conditions:
9
//
10
// The above copyright notice and this permission notice shall be included in all copies or
11
// substantial portions of the Software.
12
//
13
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
14
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18

19
import Foundation
20
import os.log
21

22
/// Representation of the Hyperwallet error list
23
public struct HyperwalletErrors: Decodable {
24
    /// The error list
25
    public private(set) var errorList: [HyperwalletError]?
26

27
    /// The original error
28
    public private(set) var originalError: Error?
29
    /// The CodingKeys for Hyperwallet errors
30
    public enum CodingKeys: String, CodingKey {
31
        /// - errorList: The list of errors
32
        case errorList = "errors"
33
    }
34

35
    /// Creates an instance of HyperwalletErrors
36
    ///
37
    /// - Parameter errorList: The `HyperwalletError` list.
38
    public init(errorList: [HyperwalletError]) {
6✔
39
        self.errorList = errorList
6✔
40
    }
6✔
41

42
    /// Creates an instance of HyperwalletErrors
43
    ///
44
    /// - Parameters:
45
    ///   - errorList: The `HyperwalletError` list.
46
    ///   - originalError: The original error.
47
    public init(errorList: [HyperwalletError], originalError: Error?) {
28✔
48
        self.errorList = errorList
28✔
49
        self.originalError = originalError
28✔
50
    }
28✔
51
}
52

53
/// Representation of the Hyperwallet error entity.
54
public struct HyperwalletError: Decodable {
55
    /// The error message
56
    public private(set) var message: String
57

58
    /// The error code
59
    public private(set) var code: String
60

61
    /// The field name
62
    public private(set) var fieldName: String?
63

64
    /// The list of related resources
65
    public private(set) var relatedResources: [String]?
66

67
    /// Creates an instance of HyperwalletError
68
    ///
69
    /// - Parameters:
70
    ///   - message: The error message
71
    ///   - code: The error code
72
    ///   - fieldName: The field name. By the default is nil
73
    ///   - relatedResources: The list of related resources. By the default is nil
74
    public init(message: String, code: String, fieldName: String? = nil, relatedResources: [String]? = nil) {
37✔
75
        self.message = message
37✔
76
        self.code = code
37✔
77
        self.fieldName = fieldName
37✔
78
        self.relatedResources = relatedResources
37✔
79
    }
37✔
80
}
81

82
/// The `HyperwalletErrorType` is the error type returned By Hyperwallet SDK.
83
public enum HyperwalletErrorType: Error, LocalizedError {
84
    /// Returned when an HTTP code is not in the range 2xx.
85
    case http(_ hyperwalletErrors: HyperwalletErrors, _ httpCode: Int)
86
    /// Returned when a response parser process throws error.
87
    case parseError(_ hyperwalletErrors: HyperwalletErrors)
88
    /// Returned when the SDK was not initialized properly.
89
    case notInitialized(_ hyperwalletErrors: HyperwalletErrors)
90
    /// Returned when a provided URL is not valid
91
    case invalidUrl(_ hyperwalletErrors: HyperwalletErrors)
92
    /// Returned when a transaction is explicitly aborted.
93
    case transactionAborted(_ hyperwalletErrors: HyperwalletErrors)
94
    /// Returned on authentication failure
95
    case authenticationError(_ authenticationError: HyperwalletAuthenticationErrorType)
96
    /// Returned when an unexpected behavior happened.
97
    case unexpected(_ hyperwalletErrors: HyperwalletErrors)
98
    /// Returned when a GraphQL parser process throws error.
99
    case graphQlErrors(_ hyperwalletErrors: HyperwalletErrors)
100
    /// Returned when some step-in builds the request throws error.
101
    case invalidRequest(_ hyperwalletErrors: HyperwalletErrors)
102
    /// Returned when during the connection process throws error.
103
    case connectionError(_ hyperwalletErrors: HyperwalletErrors)
104

105
    /// The error type group
106
    public var group: HyperwalletErrorGroup {
13✔
107
        switch self {
13✔
108
        case .http(_, let httpCode):
13✔
109
            return mapErrorGroupFor(httpCode)
9✔
110
        case .parseError,
13✔
111
             .notInitialized,
1✔
112
             .invalidUrl,
1✔
113
             .transactionAborted,
1✔
114
             .unexpected,
1✔
115
             .graphQlErrors,
1✔
116
             .invalidRequest:
1✔
117
            return HyperwalletErrorGroup.unexpected
1✔
118

13✔
119
        case .connectionError:
13✔
120
            return HyperwalletErrorGroup.connection
1✔
121

13✔
122
        case .authenticationError:
13✔
123
            return HyperwalletErrorGroup.authentication
2✔
124
        }
13✔
125
    }
13✔
126

127
    /// Gets the `HyperwalletErrors` based on the ErrorType
128
    ///
129
    /// - Returns: An instance of HyperwalletErrors or nil
130
    public func getHyperwalletErrors() -> HyperwalletErrors? {
62✔
131
        switch self {
62✔
132
        case .http(let hyperwalletErrors, _),
62✔
133
             .parseError(let hyperwalletErrors),
61✔
134
             .notInitialized(let hyperwalletErrors),
61✔
135
             .invalidUrl(let hyperwalletErrors),
61✔
136
             .transactionAborted(let hyperwalletErrors),
61✔
137
             .unexpected(let hyperwalletErrors),
61✔
138
             .graphQlErrors(let hyperwalletErrors),
61✔
139
             .invalidRequest(let hyperwalletErrors),
61✔
140
             .connectionError(let hyperwalletErrors):
61✔
141
            return hyperwalletErrors
61✔
142

62✔
143
        case .authenticationError:
62✔
144
            return nil
1✔
145
        }
62✔
146
    }
62✔
147

148
    /// Gets the `AuthenticationErrorType` on the ErrorType
149
    ///
150
    /// - Returns: An instance of AuthenticationErrorType or nil
151
    public func getAuthenticationError() -> HyperwalletAuthenticationErrorType? {
6✔
152
        switch self {
6✔
153
        case .authenticationError(let authenticationError):
6✔
154
            return authenticationError
5✔
155

6✔
156
        default:
6✔
157
            return nil
1✔
158
        }
6✔
159
    }
6✔
160

161
    /// Gets the HTTP Code based on the ErrorType.http
162
    ///
163
    /// - Returns: The HTTP Code or return nil
164
    public func getHttpCode() -> Int? {
26✔
165
        switch self {
26✔
166
        case .http(_, let httpCode):
26✔
167
            return httpCode
25✔
168

26✔
169
        default:
26✔
170
            return nil
1✔
171
        }
26✔
172
    }
26✔
173

174
    /// Maps  Hyperwallet Error Group for HTTP Code
175
    ///
176
    /// - Parameter httpCode: the reponse HTTP code
177
    /// - Returns the HyperwalletErrorGroup
178
    private func mapErrorGroupFor(_ httpCode: Int) -> HyperwalletErrorGroup {
9✔
179
        switch httpCode {
9✔
180
        case 400: return HyperwalletErrorGroup.business
9✔
181
        case 401: return HyperwalletErrorGroup.authentication
9✔
182
        default: return HyperwalletErrorGroup.unexpected
9✔
183
        }
9✔
184
    }
9✔
185
}
186

187
/// The `HyperwalletAuthenticationErrorType` is the authentication error type returned By Hyperwallet SDK.
188
public enum HyperwalletAuthenticationErrorType: LocalizedError {
189
    /// - expired: Returned when the authenticated session is expired
190
    case expired(_ message: String)
191
    /// - unexpected: Returned when an unexpected behavior happened.
192
    case unexpected(_ message: String)
193

194
    /// Gets the AuthenticationErrorType error message
195
    ///
196
    /// - Returns: An error message detail
197
    public func message() -> String {
4✔
198
        switch self {
4✔
199
        case .expired(let message):
4✔
200
            return message
1✔
201

4✔
202
        case .unexpected(let message):
4✔
203
            return message
3✔
204
        }
4✔
205
    }
4✔
206
}
207

208
/// Representation of the error type group
209
public enum HyperwalletErrorGroup: String {
210
    /// Returned when a business error is thrown
211
    case business = "BUSINESS_ERROR"
212
    /// Returned when an unexpected error is thrown
213
    case unexpected = "UNEXPECTED_ERROR"
214
    /// Returned when a connection error is thrown
215
    case connection = "CONNECTION_ERROR"
216
    /// Returned when a authentication error is thrown
217
    case authentication = "AUTHENTICATION_ERROR"
218
}
219

220
internal struct ErrorTypeHelper {
221
    private static func buildHyperwalletErrors(code: String,
222
                                               message: String,
223
                                               fieldName: String? = nil,
224
                                               error: Error? = nil) -> HyperwalletErrors {
27✔
225
        let hyperwalletError = HyperwalletError(message: message, code: code, fieldName: fieldName)
27✔
226
        let hyperwalletErrors = [hyperwalletError]
27✔
227
        return HyperwalletErrors(errorList: hyperwalletErrors, originalError: error)
27✔
228
    }
27✔
229

230
    static func parseError(message: String = "Cannot be parsed", fieldName: String? = nil) -> HyperwalletErrorType {
9✔
231
        os_log("Error occured while parsing the data", log: OSLog.data, type: .error)
9✔
232
        return HyperwalletErrorType.parseError(buildHyperwalletErrors(code: "PARSE_ERROR",
9✔
233
                                                                      message: message,
9✔
234
                                                                      fieldName: fieldName))
9✔
235
    }
9✔
236

237
    static func notInitialized(message: String = "Cannot be initialized") -> HyperwalletErrorType {
3✔
238
        os_log("%s%s%s",
3✔
239
               log: OSLog.initialization,
3✔
240
               type: .error,
3✔
241
               "Hyperwallet SDK is not initialized. ",
3✔
242
               "Call Hyperwallet.setup(provider: HyperwalletAuthenticationTokenProvider) ",
3✔
243
               "before accessing Hyperwallet.sharedInstance")
3✔
244
        return HyperwalletErrorType.notInitialized(buildHyperwalletErrors(code: "NOT_INITIALIZED", message: message))
3✔
245
    }
3✔
246

247
    static func invalidUrl(message: String = "Invalid Url") -> HyperwalletErrorType {
3✔
248
        os_log("Url is not valid", log: OSLog.httpRequest, type: .error)
3✔
249
        return HyperwalletErrorType.invalidUrl(buildHyperwalletErrors(code: "INVALID_URL", message: message))
3✔
250
    }
3✔
251

252
    static func transactionAborted(message: String = "Transaction aborted") -> HyperwalletErrorType {
1✔
253
        os_log("Transaction aborted while making Http Request", log: OSLog.httpRequest, type: .error)
1✔
254
        return HyperwalletErrorType.transactionAborted(buildHyperwalletErrors(code: "TRANSACTION_ABORTED",
1✔
255
                                                                              message: message))
1✔
256
    }
1✔
257

258
    static func unexpectedError(message: String = "Unexpected Error",
259
                                for error: Error? = nil) -> HyperwalletErrorType {
3✔
260
        os_log("Unexpected error: %s", log: OSLog.default, type: .error, message)
3✔
261
        return HyperwalletErrorType.unexpected(buildHyperwalletErrors(code: "UNEXPECTED_ERROR",
3✔
262
                                                                      message: message,
3✔
263
                                                                      error: error))
3✔
264
    }
3✔
265

266
    static func connectionError(message: String = "Please check network connection",
267
                                for error: Error? = nil) -> HyperwalletErrorType {
7✔
268
        HyperwalletErrorType.connectionError(buildHyperwalletErrors(code: "CONNECTION_ERROR",
7✔
269
                                                                    message: message,
7✔
270
                                                                    error: error))
7✔
271
    }
7✔
272

273
    static func graphQlErrors(errors: [GraphQlError]) -> HyperwalletErrorType {
3✔
274
        var hyperwalletErrors = [HyperwalletError]()
3✔
275
        for graphQlError in errors {
4✔
276
            let hyperwalletError = HyperwalletError(message: graphQlError.message ?? "Unexpected Error",
4✔
277
                                                    code: graphQlError.extensions?.code ?? "UNEXPECTED_ERROR",
4✔
278
                                                    fieldName: nil,
4✔
279
                                                    relatedResources: nil)
4✔
280
            hyperwalletErrors.append(hyperwalletError)
4✔
281
            os_log("GraphQl response contains error: %s",
4✔
282
                   log: OSLog.graphQl,
4✔
283
                   type: .info,
4✔
284
                   graphQlError.message ?? "Unexpected Error")
4✔
285
        }
4✔
286
        return HyperwalletErrorType.graphQlErrors(HyperwalletErrors(errorList: hyperwalletErrors))
3✔
287
    }
3✔
288

289
    static func invalidRequest(message: String = "invalid request", for error: Error? = nil) -> HyperwalletErrorType {
1✔
290
        os_log("Http request is invalid", log: OSLog.httpRequest, type: .error)
1✔
291
        return HyperwalletErrorType.invalidRequest(buildHyperwalletErrors(code: "INVALID_REQUEST",
1✔
292
                                                                          message: message,
1✔
293
                                                                          error: error))
1✔
294
    }
1✔
295

296
    static func authenticationError(message: String = "Authentication Error",
297
                                    for error: HyperwalletAuthenticationErrorType) -> HyperwalletErrorType {
2✔
298
        os_log("Error occured while retrieving authentication token: %s",
2✔
299
               log: OSLog.authentication,
2✔
300
               type: .error,
2✔
301
               error.localizedDescription)
2✔
302
        return HyperwalletErrorType.authenticationError(error)
2✔
303
    }
2✔
304
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc