@@ -168,7 +168,7 @@ private func adapter<T, U>(_ seal: Resolver<(data: T, response: U)>) -> (T?, U?,
168
168
169
169
170
170
#if swift(>=3.1)
171
- public enum PMKHTTPError : Error , LocalizedError {
171
+ public enum PMKHTTPError : Error , LocalizedError , CustomStringConvertible {
172
172
case badStatusCode( Int , Data , HTTPURLResponse )
173
173
174
174
public var errorDescription : String ? {
@@ -183,12 +183,44 @@ public enum PMKHTTPError: Error, LocalizedError {
183
183
}
184
184
}
185
185
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
186
194
public var jsonDictionary : Any ? {
187
195
switch self {
188
196
case . badStatusCode( _, let data, _) :
189
197
return try ? JSONSerialization . jsonObject ( with: data)
190
198
}
191
199
}
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
+ }
192
224
}
193
225
194
226
public extension Promise where T == ( data: Data , response: URLResponse ) {
0 commit comments