@@ -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
194226public extension Promise where T == ( data: Data , response: URLResponse ) {
0 commit comments