Skip to content

Commit

Permalink
Rewrite code flow to clarify error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
tclementdev committed Oct 6, 2016
1 parent df5b512 commit 2201d2c
Showing 1 changed file with 35 additions and 21 deletions.
56 changes: 35 additions & 21 deletions AFNetworking/AFURLResponseSerialization.m
Original file line number Diff line number Diff line change
Expand Up @@ -242,23 +242,28 @@ - (id)responseObjectForResponse:(NSURLResponse *)response
}
}

id responseObject = nil;
NSError *serializationError = nil;
// Workaround for behavior of Rails to return a single space for `head :ok` (a workaround for a bug in Safari), which is not interpreted as valid input by NSJSONSerialization.
// See https://github.com/rails/rails/issues/1742
BOOL isSpace = [data isEqualToData:[NSData dataWithBytes:" " length:1]];
if (data.length > 0 && !isSpace) {
responseObject = [NSJSONSerialization JSONObjectWithData:data options:self.readingOptions error:&serializationError];
} else {

if (data.length == 0 || isSpace) {
return nil;
}

NSError *serializationError = nil;

id responseObject = [NSJSONSerialization JSONObjectWithData:data options:self.readingOptions error:&serializationError];

if (self.removesKeysWithNullValues && responseObject) {
responseObject = AFJSONObjectByRemovingKeysWithNullValues(responseObject, self.readingOptions);
if (!responseObject)
{
if (error) {
*error = AFErrorWithUnderlyingError(serializationError, *error);
}
return nil;
}

if (error && !responseObject) {
*error = AFErrorWithUnderlyingError(serializationError, *error);
if (self.removesKeysWithNullValues) {
return AFJSONObjectByRemovingKeysWithNullValues(responseObject, self.readingOptions);
}

return responseObject;
Expand Down Expand Up @@ -378,10 +383,14 @@ - (id)responseObjectForResponse:(NSURLResponse *)response
NSError *serializationError = nil;
NSXMLDocument *document = [[NSXMLDocument alloc] initWithData:data options:self.options error:&serializationError];

if (error && !document) {
*error = AFErrorWithUnderlyingError(serializationError, *error);
if (!document)
{
if (error) {
*error = AFErrorWithUnderlyingError(serializationError, *error);
}
return nil;
}

return document;
}

Expand Down Expand Up @@ -458,15 +467,20 @@ - (id)responseObjectForResponse:(NSURLResponse *)response
}
}

id responseObject;
NSError *serializationError = nil;

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

if (error && !responseObject) {
*error = AFErrorWithUnderlyingError(serializationError, *error);

NSError *serializationError = nil;

id responseObject = [NSPropertyListSerialization propertyListWithData:data options:self.readOptions format:NULL error:&serializationError];

if (!responseObject)
{
if (error) {
*error = AFErrorWithUnderlyingError(serializationError, *error);
}
return nil;
}

return responseObject;
Expand Down

0 comments on commit 2201d2c

Please sign in to comment.