Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Handle Error Pointers according to Cocoa Convention #3653

Merged
merged 6 commits into from Oct 10, 2016

Conversation

tclementdev
Copy link
Contributor

This is a Cocoa convention.

@codecov-io
Copy link

codecov-io commented Aug 19, 2016

Current coverage is 87.44% (diff: 100%)

Merging #3653 into master will increase coverage by 0.81%

@@             master      #3653   diff @@
==========================================
  Files            44         45     +1   
  Lines          6067       6198   +131   
  Methods        1079       1096    +17   
  Messages          0          0          
  Branches        407        412     +5   
==========================================
+ Hits           5256       5420   +164   
+ Misses          808        775    -33   
  Partials          3          3          

Powered by Codecov. Last update e337471...f28654c

@kcharwood kcharwood closed this Sep 19, 2016
@tclementdev
Copy link
Contributor Author

I think you missed the point. The return value of the method call needs to be tested before looking at the error. This is what is done in the sample code from Apple you provided and this is not what is done in the AFNetworking code.

On 19 Sep 2016, at 17:31, Kevin Harwood notifications@github.com wrote:

Hi @tclementdev https://github.com/tclementdev! Thanks for the PR.

Apple's example docs in Dealing with Errors https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/ErrorHandling/ErrorHandling.html uses the standard we are currently using in this library, so I'm going to stick with that for now.

NSError *anyError;
BOOL success = [receivedData writeToURL:someLocalFileURL
                                options:0
                                  error:&anyError];
if (!success) {
    NSLog(@"Write failed with error: %@", anyError);
    // present error to user
}

🍻


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub #3653 (comment), or mute the thread https://github.com/notifications/unsubscribe-auth/ABWubYuOFTgfx9rynV9nLWZhSOhu7AQtks5qrqrBgaJpZM4Joc7m.

@tclementdev
Copy link
Contributor Author

@kcharwood

@0xced
Copy link
Contributor

0xced commented Sep 30, 2016

I don’t understand why this PR was closed, @tclementdev point is perfectly valid.

From Cocoa's Error Handling Programming Guide

Important: Success or failure is indicated by the return value of the method. Although Cocoa methods that indirectly return error objects in the Cocoa error domain are guaranteed to return such objects if the method indicates failure by directly returning nil or NO, you should always check that the return value is nil or NO before attempting to do anything with the NSError object.

@kcharwood
Copy link
Contributor

Sorry misread the original intent! Reopened here.

Has a full audit of the codebase been done for this particular issue?

@kcharwood kcharwood reopened this Oct 3, 2016
@tclementdev
Copy link
Contributor Author

Thank you for re-opening this PR. I just pushed some additionnal fixes concerning the same issue in other system API calls.

I could not find any other system API misuse, however it should be noted that the AFNetworking APIs themselves seem to be quite oblivious of this convention, with code seemingly returning non-nil objects while an error occurred internally. There is probably some amount of work to audit and modify the AFNetworking code itself to clarify and address this (this can be done in a separate PR).

@kcharwood
Copy link
Contributor

Can you rebase this with master to get Xcode 8 coverage?

@kcharwood kcharwood added this to the 3.2.0 milestone Oct 5, 2016
@tclementdev
Copy link
Contributor Author

Done.

@kcharwood
Copy link
Contributor

You can see the codecov diff here.

Verifying those nil's should help prevent regressions in the future. After that, should be good to merge!

return nil;
}

[mutableRequest setHTTPBody:jsonData];
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a test verifying the nil behavior here?


if (!plistData) {
return nil;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Also a test to verify nil behavior here. Just so we can catch regressions in the future.

if (error) {
*error = AFErrorWithUnderlyingError(serializationError, *error);
}
return nil;
Copy link
Contributor

Choose a reason for hiding this comment

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

Test verifying nil

if (error) {
*error = AFErrorWithUnderlyingError(serializationError, *error);
}
return nil;
Copy link
Contributor

Choose a reason for hiding this comment

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

Test verifying nil

if (data) {
responseObject = [NSPropertyListSerialization propertyListWithData:data options:self.readOptions format:NULL error:&serializationError];
if (!data) {
return nil;
Copy link
Contributor

Choose a reason for hiding this comment

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

Test verifying nil

@tclementdev
Copy link
Contributor Author

Am I good?

@kcharwood kcharwood changed the title Check return value before doing anything with error. Handle Error Pointers according to Cocoa Convention Oct 10, 2016
@kcharwood kcharwood merged commit e8fde52 into AFNetworking:master Oct 10, 2016
@kcharwood
Copy link
Contributor

🍻 thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
4 participants