Skip to content

Commit 80dc67a

Browse files
committed
NetworkError: implement optional raw Data
1 parent 32dac9b commit 80dc67a

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

CodyFire.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = 'CodyFire'
11-
s.version = '1.7.2'
11+
s.version = '1.7.3'
1212
s.summary = '❤️ Powerful codable API requests builder and manager for iOS based on Alamofire'
1313

1414
# This description is used to generate tags and improve search results.

CodyFire/Classes/APIRequest+ParseError.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ extension APIRequest {
2222
errorCallback?(globalCustomError)
2323
}
2424
} else {
25-
errorCallback?(NetworkError(code: statusCode, description: description + "(\(statusCode.rawValue))"))
25+
errorCallback?(NetworkError(code: statusCode, description: description + "(\(statusCode.rawValue))", raw: data))
2626
}
2727
}
2828
}

CodyFire/Classes/NetworkError.swift

+8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ public typealias KnownNetworkError = NetworkError
1313
public struct NetworkError: Error, CustomStringConvertible {
1414
public var code: StatusCode
1515
public var description: String
16+
public var raw: Data?
17+
18+
init (code: StatusCode, description: String, raw: Data?) {
19+
self.code = code
20+
self.description = description
21+
self.raw = raw
22+
}
23+
1624
public init (code: StatusCode, description: String) {
1725
self.code = code
1826
self.description = description

README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ and you're able to handle cancellation
518518
#### What does custom error means?
519519

520520
You may define your own custom errors, globally or for each request.
521-
`onError` block contains `NetworkError` object with `StatusCode` enum and an error description, so that description you could change to whatever you want for any error code.
521+
`onError` block contains `NetworkError` object with `StatusCode` enum, an error description, and a raw response `Data`. Error description you could change to whatever you want for any error code.
522522
By default there are already defined some good descriptions for common errors.
523523

524524
Let's take a look how we can use powerful `onError` block
@@ -529,7 +529,11 @@ Let's take a look how we can use powerful `onError` block
529529
case .internalServerError: print("Oooops... Something really went wrong...")
530530
case .custom(let code): print("My non-standard-custom error happened: " + error.description)
531531
case .unknown(let code): print("Totally unknown error happened: " + error.description)
532-
default: print("Another error happened: " + error.description)
532+
default:
533+
print("Another error happened: " + error.description)
534+
if let raw = error.raw, let rawResponse = String(data: raw, encoding: .utf8) {
535+
print("Raw response: " + rawResponse)
536+
}
533537
}
534538
}
535539
```
@@ -680,7 +684,7 @@ API.employee.all()
680684
`onRequestStarted, onNetworkUnavailable, onCancellation, onNotAuthorized, onTimeout also available!`
681685
`//TBD: onProgress`
682686

683-
I believe it is awesome! Especially for whom who not familiar or don't like reactive programming 🙂
687+
I believe it is awesome! Especially for whom who not familiar or don't like reactive programming 🙂
684688

685689
### Flatten
686690

0 commit comments

Comments
 (0)