diff --git a/HTTPService/Mapping/HTTPObjectMapping.swift b/HTTPService/Mapping/HTTPObjectMapping.swift index 238ff96..9fb5466 100644 --- a/HTTPService/Mapping/HTTPObjectMapping.swift +++ b/HTTPService/Mapping/HTTPObjectMapping.swift @@ -47,7 +47,7 @@ public class HTTPObjectMapping { let response = box.value let successRange = request.acceptibleStatusCodeRange if !successRange.contains(response.statusCode) { - return .Failure(NSError(domain: "HTTPObjectMappingErrorDomain", code: 8989, userInfo: [NSLocalizedDescriptionKey: "Response status code not is acceptable range"])) + return .Failure(NSError(domain: "HTTPObjectMappingErrorDomain", code: 8989, userInfo: [NSLocalizedDescriptionKey: "Response status code not in acceptable range"])) } return .Success(Box(response.data)) case let .Failure(error): diff --git a/HTTPService/Operations/HTTPRequestOperation.swift b/HTTPService/Operations/HTTPRequestOperation.swift index 9376df9..6b55c09 100644 --- a/HTTPService/Operations/HTTPRequestOperation.swift +++ b/HTTPService/Operations/HTTPRequestOperation.swift @@ -128,7 +128,27 @@ public class HTTPRequestOperation: NSOperation { self.responseData = data self.error = error } else { - self.error = NSError(domain: self.errorDomain, code: 3333, userInfo: [NSLocalizedDescriptionKey: "Status code received (\(statusCode)) was not within acceptable range (\(self.request.acceptibleStatusCodeRange.startIndex))-(\(self.request.acceptibleStatusCodeRange.endIndex))"]) + + guard let _data = data else { + self.error = self.invlaidStatusCodeError(statusCode) + self.cancel() + return + } + + do { + let errorJSON = try NSJSONSerialization.JSONObjectWithData(_data, options: NSJSONReadingOptions(rawValue: 0)) + guard let _error = errorJSON["error"] as? String else { + self.error = self.invlaidStatusCodeError(statusCode) + self.cancel() + return + } + + self.error = NSError(domain: self.errorDomain, code: 9393, userInfo: [NSLocalizedDescriptionKey: _error]) + + } catch _ { + self.error = self.invlaidStatusCodeError(statusCode) + } + self.cancel() } } else { @@ -170,6 +190,10 @@ public class HTTPRequestOperation: NSOperation { finished = true } + func invlaidStatusCodeError(statusCode: Int) -> NSError { + return NSError(domain: self.errorDomain, code: 3333, userInfo: [NSLocalizedDescriptionKey: "Status code received (\(statusCode)) was not within acceptable range (\(self.request.acceptibleStatusCodeRange.startIndex))-(\(self.request.acceptibleStatusCodeRange.endIndex))"]) + } + } extension HTTPRequestOperation: NSURLSessionDelegate {