Skip to content

Commit

Permalink
Better error message handling
Browse files Browse the repository at this point in the history
  • Loading branch information
atljeremy committed Jun 19, 2016
1 parent 2e957bc commit cca2704
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion HTTPService/Mapping/HTTPObjectMapping.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
26 changes: 25 additions & 1 deletion HTTPService/Operations/HTTPRequestOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit cca2704

Please sign in to comment.