Skip to content

Commit d6a7622

Browse files
committed
Improve custom errors
1 parent cbe36c2 commit d6a7622

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

CodyFire/Classes/APIRequest+Build.swift

+15-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ extension APIRequest {
9393

9494
@discardableResult
9595
public func addCustomError(_ customError: NetworkError) -> APIRequest {
96-
customErrors.append(customError)
96+
customErrors.update(with:customError)
9797
return self
9898
}
9999

@@ -103,6 +103,19 @@ extension APIRequest {
103103
return addCustomError(code, description)
104104
}
105105

106+
@discardableResult
107+
public func addCustomError(_ codes: StatusCode..., description: String) -> APIRequest {
108+
return addCustomError(codes, description: description)
109+
}
110+
111+
@discardableResult
112+
public func addCustomError(_ codes: [StatusCode], description: String) -> APIRequest {
113+
for code in codes {
114+
addCustomError(NetworkError(code: code, description: description))
115+
}
116+
return self
117+
}
118+
106119
@discardableResult
107120
public func addCustomError(_ code: StatusCode, _ description: String) -> APIRequest {
108121
return addCustomError(NetworkError(code: code, description: description))
@@ -116,7 +129,7 @@ extension APIRequest {
116129

117130
@discardableResult
118131
public func addCustomErrors(_ errors: [NetworkError]) -> APIRequest {
119-
customErrors.append(contentsOf: errors)
132+
errors.forEach { customErrors.update(with: $0) }
120133
return self
121134
}
122135

CodyFire/Classes/APIRequest.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class APIRequest<ResultType: Decodable> {
3636

3737
public typealias SuccessResponse = (ResultType)->()
3838
var customServerURL: ServerURL?
39-
var customErrors: [NetworkError] = []
39+
var customErrors: Set<NetworkError> = []
4040
var endpoint: String = "/"
4141
var method: HTTPMethod = .get
4242
var payload: PayloadProtocol?

CodyFire/Classes/CodyFire.swift

+12-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ open class CodyFire {
2626

2727
/// Called on each succeeded response
2828
public var successResponseHandler: ((_ host: String, _ endpoint: String)->())?
29+
public var errorResponseHandler: ((_ host: String, _ endpoint: String)->())?
2930
public var unauthorizedHandler: UnauthorizedHandler?
3031
public var reachability: NetworkHelperProtocol?
3132

@@ -52,7 +53,7 @@ open class CodyFire {
5253

5354
public var ws: WS { return WS.shared }
5455

55-
var customErrors: [NetworkError] = [
56+
var customErrors: Set<NetworkError> = [
5657
NetworkError(code: .unauthorized, description: "You're not authorized"),
5758
NetworkError(code: .forbidden, description: "This action is prohibited"),
5859
NetworkError(code: .internalServerError, description: "Service temporary unavailable"),
@@ -81,9 +82,16 @@ open class CodyFire {
8182
]
8283

8384
public func setCustomError(_ error: NetworkError) {
84-
if let index = customErrors.index(where: { $0.code.rawValue == error.code.rawValue }) {
85-
customErrors.remove(at: index)
86-
customErrors.append(error)
85+
customErrors.update(with: error)
86+
}
87+
88+
public func setCustomError(codes: StatusCode..., description: String) {
89+
setCustomError(codes: codes, description: description)
90+
}
91+
92+
public func setCustomError(codes: [StatusCode], description: String) {
93+
for code in codes {
94+
setCustomError(NetworkError(code: code, description: description))
8795
}
8896
}
8997

CodyFire/Classes/NetworkError.swift

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Foundation
1010
@available(*, deprecated, renamed: "NetworkError")
1111
public typealias KnownNetworkError = NetworkError
1212

13-
public struct NetworkError: Error, CustomStringConvertible {
13+
public struct NetworkError: Error, CustomStringConvertible, Hashable {
1414
public var code: StatusCode
1515
public var description: String
1616
public var raw: Data?
@@ -25,4 +25,8 @@ public struct NetworkError: Error, CustomStringConvertible {
2525
self.code = code
2626
self.description = description
2727
}
28+
29+
public func hash(into hasher: inout Hasher) {
30+
hasher.combine(code.rawValue)
31+
}
2832
}

0 commit comments

Comments
 (0)