diff --git a/.travis.yml b/.travis.yml index 6415a03..16c4e88 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/Sources/NSURLSession+Promise.swift b/Sources/NSURLSession+Promise.swift index 7e06c5a..926eadf 100644 --- a/Sources/NSURLSession+Promise.swift +++ b/Sources/NSURLSession+Promise.swift @@ -168,7 +168,7 @@ private func adapter(_ 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? { @@ -183,12 +183,44 @@ public enum PMKHTTPError: Error, LocalizedError { } } + public func decodeResponse(_ 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 " \(NSDictionary(dictionary: dict))" // as NSDictionary makes the output look like NSHTTPURLResponse looks + } + } } public extension Promise where T == (data: Data, response: URLResponse) {