Skip to content

Commit 500fdbf

Browse files
authored
Supplementing our PMKHTTPError (#11)
1 parent e052c5e commit 500fdbf

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

.travis.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,16 @@ matrix:
4242
- {osx_image: xcode10, env: 'SWFT=4.3 PLAT=macOS DST="arch=x86_64"', os: osx, language: objective-c}
4343
- {osx_image: xcode10, env: 'SWFT=4.3 PLAT=watchOS DST="OS=5.0,name=Apple Watch Series 3 - 42mm"', os: osx, language: objective-c}
4444

45-
# Swift 3.2.0
45+
# Swift 3.2.0 (we have some source-conditionals for this version)
4646
- {os: linux, dist: trusty, sudo: required, language: generic, env: 'SWIFT_BUILD_VERSION=3 SWIFT_VERSION=4.0'}
47-
# Swift 3.2.2 (because 3.2.1 isn't available with swift-env and we need to verify some source code conditionals)
48-
- {os: linux, dist: trusty, sudo: required, language: generic, env: 'SWIFT_BUILD_VERSION=3 SWIFT_VERSION=4.0.2'}
4947
# Swift 3.2.3
5048
- {os: linux, dist: trusty, sudo: required, language: generic, env: 'SWIFT_BUILD_VERSION=3 SWIFT_VERSION=4.0.3'}
5149
# Swift 3.3
5250
- {os: linux, dist: trusty, sudo: required, language: generic, env: 'SWIFT_BUILD_VERSION=3 SWIFT_VERSION=4.1.2 TEST=1'}
5351
# Swift 3.4
5452
- {os: linux, dist: trusty, sudo: required, language: generic, env: 'SWIFT_BUILD_VERSION=3 SWIFT_VERSION=DEVELOPMENT-SNAPSHOT-2018-06-20-a TEST=1'}
55-
# Swift 4.0.0
53+
# Swift 4.0.0 (we have some source-conditionals for this version)
5654
- {os: linux, dist: trusty, sudo: required, language: generic, env: 'SWIFT_BUILD_VERSION=4 SWIFT_VERSION=4.0'}
57-
# Swift 4.0.2 (because 4.0.1 isn't available with swift-env and we need to verify some source code conditionals)
58-
- {os: linux, dist: trusty, sudo: required, language: generic, env: 'SWIFT_BUILD_VERSION=4 SWIFT_VERSION=4.0.2'}
5955
# Swift 4.0.3
6056
- {os: linux, dist: trusty, sudo: required, language: generic, env: 'SWIFT_BUILD_VERSION=4 SWIFT_VERSION=4.0.3'}
6157
# Swift 4.1

Sources/NSURLSession+Promise.swift

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ private func adapter<T, U>(_ seal: Resolver<(data: T, response: U)>) -> (T?, U?,
168168

169169

170170
#if swift(>=3.1)
171-
public enum PMKHTTPError: Error, LocalizedError {
171+
public enum PMKHTTPError: Error, LocalizedError, CustomStringConvertible {
172172
case badStatusCode(Int, Data, HTTPURLResponse)
173173

174174
public var errorDescription: String? {
@@ -183,12 +183,44 @@ public enum PMKHTTPError: Error, LocalizedError {
183183
}
184184
}
185185

186+
public func decodeResponse<T: Decodable>(_ t: T.Type, decoder: JSONDecoder = JSONDecoder()) -> T? {
187+
switch self {
188+
case .badStatusCode(_, let data, _):
189+
return try? decoder.decode(t, from: data)
190+
}
191+
}
192+
193+
//TODO rename responseJSON
186194
public var jsonDictionary: Any? {
187195
switch self {
188196
case .badStatusCode(_, let data, _):
189197
return try? JSONSerialization.jsonObject(with: data)
190198
}
191199
}
200+
201+
var responseBodyString: String? {
202+
switch self {
203+
case .badStatusCode(_, let data, _):
204+
return String(data: data, encoding: .utf8)
205+
}
206+
}
207+
208+
public var failureReason: String? {
209+
return responseBodyString
210+
}
211+
212+
public var description: String {
213+
switch self {
214+
case .badStatusCode(let code, let data, let response):
215+
var dict: [String: Any] = [
216+
"Status Code": code,
217+
"Body": String(data: data, encoding: .utf8) ?? "\(data.count) bytes"
218+
]
219+
dict["URL"] = response.url
220+
dict["Headers"] = response.allHeaderFields
221+
return "<NSHTTPResponse> \(NSDictionary(dictionary: dict))" // as NSDictionary makes the output look like NSHTTPURLResponse looks
222+
}
223+
}
192224
}
193225

194226
public extension Promise where T == (data: Data, response: URLResponse) {

0 commit comments

Comments
 (0)