 		do {
 			payload = try serializer.serializeResources([resource], options: options)
 		} catch let error {
-			result = .failure(error as! SpineError)
+			result = .failure(error.asSpineError)
 			state = .finished
 			return
 		}

			defer { self.state = .finished }
		
			guard networkError == nil else {
				self.result = Failable.failure(SpineError.networkError(networkError!))
				return
			}
			
			if statusCodeIsSuccess(statusCode) {
				self.result = Failable.success()
			} else if let data = responseData , data.count > 0 {
				do {
					let document = try self.serializer.deserializeData(data, mappingTargets: nil)
					self.result = .failure(SpineError.serverError(statusCode: statusCode!, apiErrors: document.errors))
				} catch let error {
					self.result = .failure(promoteToSpineError(error))
				}
			} else {
				self.result = .failure(errorFromStatusCode(statusCode!))
			}
		}
	}


import Foundation

fileprivate func statusCodeIsSuccess(_ statusCode: Int?) -> Bool {
	return statusCode != nil && 200 ... 299 ~= statusCode!
}

fileprivate extension Error {
	///  Promotes an ErrorType to a higher level SpineError.
	///  Errors that cannot be represented as a SpineError will be returned as SpineError.UnknownError
	var asSpineError: SpineError {
		switch self {
		case is SpineError:
			return self as! SpineError
		case is SerializerError:
			return .serializerError
		default:
			return .unknownError
		}
	}
}


// MARK: - Base operation

