Skip to content

Commit

Permalink
NetworkError: implement optional raw Data
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaelIsaev committed Nov 5, 2018
1 parent 32dac9b commit 80dc67a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CodyFire.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

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

# This description is used to generate tags and improve search results.
Expand Down
2 changes: 1 addition & 1 deletion CodyFire/Classes/APIRequest+ParseError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extension APIRequest {
errorCallback?(globalCustomError)
}
} else {
errorCallback?(NetworkError(code: statusCode, description: description + "(\(statusCode.rawValue))"))
errorCallback?(NetworkError(code: statusCode, description: description + "(\(statusCode.rawValue))", raw: data))
}
}
}
8 changes: 8 additions & 0 deletions CodyFire/Classes/NetworkError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ public typealias KnownNetworkError = NetworkError
public struct NetworkError: Error, CustomStringConvertible {
public var code: StatusCode
public var description: String
public var raw: Data?

init (code: StatusCode, description: String, raw: Data?) {
self.code = code
self.description = description
self.raw = raw
}

public init (code: StatusCode, description: String) {
self.code = code
self.description = description
Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ and you're able to handle cancellation
#### What does custom error means?

You may define your own custom errors, globally or for each request.
`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.
`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.
By default there are already defined some good descriptions for common errors.

Let's take a look how we can use powerful `onError` block
Expand All @@ -529,7 +529,11 @@ Let's take a look how we can use powerful `onError` block
case .internalServerError: print("Oooops... Something really went wrong...")
case .custom(let code): print("My non-standard-custom error happened: " + error.description)
case .unknown(let code): print("Totally unknown error happened: " + error.description)
default: print("Another error happened: " + error.description)
default:
print("Another error happened: " + error.description)
if let raw = error.raw, let rawResponse = String(data: raw, encoding: .utf8) {
print("Raw response: " + rawResponse)
}
}
}
```
Expand Down Expand Up @@ -680,7 +684,7 @@ API.employee.all()
`onRequestStarted, onNetworkUnavailable, onCancellation, onNotAuthorized, onTimeout also available!`
`//TBD: onProgress`

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

### Flatten

Expand Down

0 comments on commit 80dc67a

Please sign in to comment.