Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Error Handling #1419

Merged
merged 11 commits into from Aug 29, 2016
Merged

New Error Handling #1419

merged 11 commits into from Aug 29, 2016

Conversation

jshier
Copy link
Contributor

@jshier jshier commented Aug 22, 2016

This PR refactors Alamofire's error handling to be based around a new AFError type rather than NSError, as Swift 3 has pushed the system frameworks and consumers toward using Error for everything.

AFError consists of a few base cases with reason types for each. Convenience booleans and other properties have been added to make interacting with type easier in end use scenarios.

This PR is a works in progress as the inline and separate documentation still needs to be completed and the tests need to be updated for the new error type.

@jshier
Copy link
Contributor Author

jshier commented Aug 22, 2016

@cnoon One unanswered question to also consider is whether the response serialization errors should wrap the underlying decoding errors (e.g. the JSONSerialization and PropertyListSerialization errors).

setBodyPartError(withCode: NSURLErrorBadURL, failureReason: "The file URL is not reachable: \(fileURL)")

do {
let isReachable = try fileURL.checkPromisedItemIsReachable()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newline?

@cnoon
Copy link
Member

cnoon commented Aug 24, 2016

@jshier...I'd say there's a couple open questions about the errors themselves. What to do with JSON and plist errors and also what to do with request errors. In both cases, I'm leaning towards wrapping them up in an AFError. It makes it MUCH easier for the consumer to switch on the top-level types of errors without have to switch between AFError and NSError. If they really need to drop down another level, they easily can.

case responseValidationFailed(reason: ValidationFailureReason)
case responseSerializationFailed(reason: SerializationFailureReason)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra newline.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

if let error = error as? AFError, let contentType = error.responseContentType, let acceptableTypes = error.acceptableContentTypes {
XCTAssertTrue(error.isUnacceptableContentType)
XCTAssertEqual(contentType, "application/xml")
XCTAssertEqual(acceptableTypes[0], "application/octet-stream")
} else {
XCTFail("error should not be nil")
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will crash if acceptableTypes is empty. Could we get a check for the index as well as blow out the XCTFail explanation to explain the other cases where this could fail?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the acceptable type into the let statements and updated all of the failure descriptions to describe what the if statement should be unwrapping.

@jshier jshier changed the title WIP: New Error Handling New Error Handling Aug 28, 2016
@cnoon cnoon merged commit 7b419b7 into swift3 Aug 29, 2016
@cnoon cnoon removed the in progress label Aug 29, 2016
@cnoon cnoon self-assigned this Aug 29, 2016
@cnoon cnoon added the errors label Aug 29, 2016
@cnoon cnoon added this to the 4.0.0 milestone Aug 29, 2016
@jshier jshier deleted the new-error-handling branch August 29, 2016 00:57
cnoon pushed a commit that referenced this pull request Sep 8, 2016
* Preliminary refactoring.

* Refactor to use AFError.

* Update inline docs in SessionDelegate.

* Update AFError implementation.

* Start trying to fix tests.

* Update and fix tests.

* Address comments.

* Remove unnecessary @testable declarations.

* Cleanup whitespace.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants