Skip to content

Commit

Permalink
Supplementing our PMKHTTPError (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxcl authored Aug 20, 2018
1 parent e052c5e commit 500fdbf
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
8 changes: 2 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,16 @@ matrix:
- {osx_image: xcode10, env: 'SWFT=4.3 PLAT=macOS DST="arch=x86_64"', os: osx, language: objective-c}
- {osx_image: xcode10, env: 'SWFT=4.3 PLAT=watchOS DST="OS=5.0,name=Apple Watch Series 3 - 42mm"', os: osx, language: objective-c}

# Swift 3.2.0
# Swift 3.2.0 (we have some source-conditionals for this version)
- {os: linux, dist: trusty, sudo: required, language: generic, env: 'SWIFT_BUILD_VERSION=3 SWIFT_VERSION=4.0'}
# Swift 3.2.2 (because 3.2.1 isn't available with swift-env and we need to verify some source code conditionals)
- {os: linux, dist: trusty, sudo: required, language: generic, env: 'SWIFT_BUILD_VERSION=3 SWIFT_VERSION=4.0.2'}
# Swift 3.2.3
- {os: linux, dist: trusty, sudo: required, language: generic, env: 'SWIFT_BUILD_VERSION=3 SWIFT_VERSION=4.0.3'}
# Swift 3.3
- {os: linux, dist: trusty, sudo: required, language: generic, env: 'SWIFT_BUILD_VERSION=3 SWIFT_VERSION=4.1.2 TEST=1'}
# Swift 3.4
- {os: linux, dist: trusty, sudo: required, language: generic, env: 'SWIFT_BUILD_VERSION=3 SWIFT_VERSION=DEVELOPMENT-SNAPSHOT-2018-06-20-a TEST=1'}
# Swift 4.0.0
# Swift 4.0.0 (we have some source-conditionals for this version)
- {os: linux, dist: trusty, sudo: required, language: generic, env: 'SWIFT_BUILD_VERSION=4 SWIFT_VERSION=4.0'}
# Swift 4.0.2 (because 4.0.1 isn't available with swift-env and we need to verify some source code conditionals)
- {os: linux, dist: trusty, sudo: required, language: generic, env: 'SWIFT_BUILD_VERSION=4 SWIFT_VERSION=4.0.2'}
# Swift 4.0.3
- {os: linux, dist: trusty, sudo: required, language: generic, env: 'SWIFT_BUILD_VERSION=4 SWIFT_VERSION=4.0.3'}
# Swift 4.1
Expand Down
34 changes: 33 additions & 1 deletion Sources/NSURLSession+Promise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private func adapter<T, U>(_ seal: Resolver<(data: T, response: U)>) -> (T?, U?,


#if swift(>=3.1)
public enum PMKHTTPError: Error, LocalizedError {
public enum PMKHTTPError: Error, LocalizedError, CustomStringConvertible {
case badStatusCode(Int, Data, HTTPURLResponse)

public var errorDescription: String? {
Expand All @@ -183,12 +183,44 @@ public enum PMKHTTPError: Error, LocalizedError {
}
}

public func decodeResponse<T: Decodable>(_ t: T.Type, decoder: JSONDecoder = JSONDecoder()) -> T? {
switch self {
case .badStatusCode(_, let data, _):
return try? decoder.decode(t, from: data)
}
}

//TODO rename responseJSON
public var jsonDictionary: Any? {
switch self {
case .badStatusCode(_, let data, _):
return try? JSONSerialization.jsonObject(with: data)
}
}

var responseBodyString: String? {
switch self {
case .badStatusCode(_, let data, _):
return String(data: data, encoding: .utf8)
}
}

public var failureReason: String? {
return responseBodyString
}

public var description: String {
switch self {
case .badStatusCode(let code, let data, let response):
var dict: [String: Any] = [
"Status Code": code,
"Body": String(data: data, encoding: .utf8) ?? "\(data.count) bytes"
]
dict["URL"] = response.url
dict["Headers"] = response.allHeaderFields
return "<NSHTTPResponse> \(NSDictionary(dictionary: dict))" // as NSDictionary makes the output look like NSHTTPURLResponse looks
}
}
}

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

0 comments on commit 500fdbf

Please sign in to comment.