Serializer error for Alamofire
I'm trying to write generic serializers for Alamofire, and I'm trying to figure out how I can get the response serializer generator to detect if there was an error, and instead return a custom error object (the server returns a dictionary of errors in this situation).
Any input would be appreciated!
Here's some sample code I'm currently using:
@objc public protocol ResponseObjectSerializable {
init?(response: NSHTTPURLResponse, representation: AnyObject)
}
extension Alamofire.Request {
public func responseObject<T: ResponseObjectSerializable>(completionHandler: (NSURLRequest, NSHTTPURLResponse?, T?, NSError?) -> Void) -> Self {
let serializer: Serializer = { (request, response, data) in
let JSONSerializer = Request.JSONResponseSerializer(options: .AllowFragments)
let (JSON: AnyObject?, serializationError) = JSONSerializer(request, response, data)
if response != nil && JSON != nil {
return (T(response: response!, representation: JSON!), nil)
} else {
return (nil, serializationError)
}
}
return response(serializer: serializer, completionHandler: { (request, response, object, error) in
completionHandler(request, response, object, error)
})
}
}
+3
source to share
No one has answered this question yet
Check out similar questions: