Skip to content

Commit

Permalink
Uses local error domain and local error generation function
Browse files Browse the repository at this point in the history
  • Loading branch information
andre-wolfe committed Jun 29, 2016
1 parent 72c1d0c commit 45b864b
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions AlamofireObjectMapper/AlamofireObjectMapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ import ObjectMapper

extension Request {

internal static func newError(code: Error.Code, failureReason: String) -> NSError {
let errorDomain = "com.alamofireobjectmapper.error"

let userInfo = [NSLocalizedFailureReasonErrorKey: failureReason]
let returnError = NSError(domain: errorDomain, code: code.rawValue, userInfo: userInfo)

return returnError
}

public static func ObjectMapperSerializer<T: Mappable>(keyPath: String?, mapToObject object: T? = nil, context: MapContext? = nil) -> ResponseSerializer<T, NSError> {
return ResponseSerializer { request, response, data, error in
guard error == nil else {
Expand All @@ -40,7 +49,7 @@ extension Request {

guard let _ = data else {
let failureReason = "Data could not be serialized. Input data was nil."
let error = Error.errorWithCode(.DataSerializationFailed, failureReason: failureReason)
let error = newError(.DataSerializationFailed, failureReason: failureReason)
return .Failure(error)
}

Expand All @@ -62,7 +71,7 @@ extension Request {
}

let failureReason = "ObjectMapper failed to serialize response."
let error = Error.errorWithCode(.DataSerializationFailed, failureReason: failureReason)
let error = newError(.DataSerializationFailed, failureReason: failureReason)
return .Failure(error)
}
}
Expand Down Expand Up @@ -90,7 +99,7 @@ extension Request {

guard let _ = data else {
let failureReason = "Data could not be serialized. Input data was nil."
let error = Error.errorWithCode(.DataSerializationFailed, failureReason: failureReason)
let error = newError(.DataSerializationFailed, failureReason: failureReason)
return .Failure(error)
}

Expand All @@ -109,7 +118,7 @@ extension Request {
}

let failureReason = "ObjectMapper failed to serialize response."
let error = Error.errorWithCode(.DataSerializationFailed, failureReason: failureReason)
let error = newError(.DataSerializationFailed, failureReason: failureReason)
return .Failure(error)
}
}
Expand Down

0 comments on commit 45b864b

Please sign in to comment.