Просмотр исходного кода

Make sure we check the return values before handling the error objects (this is the correct way to handle errors as described in Apple's documentation).

Thomas Clement 9 лет назад
Родитель
Сommit
df5b5121a4
2 измененных файлов с 17 добавлено и 5 удалено
  1. 14 2
      AFNetworking/AFURLRequestSerialization.m
  2. 3 3
      AFNetworking/AFURLResponseSerialization.m

+ 14 - 2
AFNetworking/AFURLRequestSerialization.m

@@ -1254,7 +1254,13 @@ typedef enum {
             [mutableRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
             [mutableRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
         }
         }
 
 
-        [mutableRequest setHTTPBody:[NSJSONSerialization dataWithJSONObject:parameters options:self.writingOptions error:error]];
+        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters options:self.writingOptions error:error];
+        
+        if (!jsonData) {
+            return nil;
+        }
+        
+        [mutableRequest setHTTPBody:jsonData];
     }
     }
 
 
     return mutableRequest;
     return mutableRequest;
@@ -1333,7 +1339,13 @@ typedef enum {
             [mutableRequest setValue:@"application/x-plist" forHTTPHeaderField:@"Content-Type"];
             [mutableRequest setValue:@"application/x-plist" forHTTPHeaderField:@"Content-Type"];
         }
         }
 
 
-        [mutableRequest setHTTPBody:[NSPropertyListSerialization dataWithPropertyList:parameters format:self.format options:self.writeOptions error:error]];
+        NSData *plistData = [NSPropertyListSerialization dataWithPropertyList:parameters format:self.format options:self.writeOptions error:error];
+        
+        if (!plistData) {
+            return nil;
+        }
+        
+        [mutableRequest setHTTPBody:plistData];
     }
     }
 
 
     return mutableRequest;
     return mutableRequest;

+ 3 - 3
AFNetworking/AFURLResponseSerialization.m

@@ -257,7 +257,7 @@ static id AFJSONObjectByRemovingKeysWithNullValues(id JSONObject, NSJSONReadingO
         responseObject = AFJSONObjectByRemovingKeysWithNullValues(responseObject, self.readingOptions);
         responseObject = AFJSONObjectByRemovingKeysWithNullValues(responseObject, self.readingOptions);
     }
     }
 
 
-    if (error) {
+    if (error && !responseObject) {
         *error = AFErrorWithUnderlyingError(serializationError, *error);
         *error = AFErrorWithUnderlyingError(serializationError, *error);
     }
     }
 
 
@@ -378,7 +378,7 @@ static id AFJSONObjectByRemovingKeysWithNullValues(id JSONObject, NSJSONReadingO
     NSError *serializationError = nil;
     NSError *serializationError = nil;
     NSXMLDocument *document = [[NSXMLDocument alloc] initWithData:data options:self.options error:&serializationError];
     NSXMLDocument *document = [[NSXMLDocument alloc] initWithData:data options:self.options error:&serializationError];
 
 
-    if (error) {
+    if (error && !document) {
         *error = AFErrorWithUnderlyingError(serializationError, *error);
         *error = AFErrorWithUnderlyingError(serializationError, *error);
     }
     }
 
 
@@ -465,7 +465,7 @@ static id AFJSONObjectByRemovingKeysWithNullValues(id JSONObject, NSJSONReadingO
         responseObject = [NSPropertyListSerialization propertyListWithData:data options:self.readOptions format:NULL error:&serializationError];
         responseObject = [NSPropertyListSerialization propertyListWithData:data options:self.readOptions format:NULL error:&serializationError];
     }
     }
 
 
-    if (error) {
+    if (error && !responseObject) {
         *error = AFErrorWithUnderlyingError(serializationError, *error);
         *error = AFErrorWithUnderlyingError(serializationError, *error);
     }
     }