Permalink
Browse files

Much cleaner error handling.

  • Loading branch information...
1 parent 5a0809c commit 6416929e56cb99ef6fd47669f22abb05e59dfcd9 @schwa schwa committed Jul 23, 2011
Showing with 34 additions and 81 deletions.
  1. +34 −81 Source/JSON/CJSONScanner.m
View
@@ -48,8 +48,11 @@ inline static int HexToInt(char inCharacter)
static id kNSYES = NULL;
static id kNSNO = NULL;
+
@interface CJSONScanner ()
- (BOOL)scanNotQuoteCharactersIntoString:(NSString **)outValue;
+
+- (NSError *)error:(NSInteger)inCode description:(NSString *)inDescription;
@end
#pragma mark -
@@ -132,11 +135,7 @@ - (BOOL)setData:(NSData *)inData error:(NSError **)outError;
{
if (outError)
{
- NSMutableDictionary *theUserInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
- @"Could not scan data. Data wasn't encoded properly?", NSLocalizedDescriptionKey,
- NULL];
- [theUserInfo addEntriesFromDictionary:self.userInfoForScanLocation];
- *outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:kJSONScannerErrorCode_CouldNotDecodeData userInfo:theUserInfo];
+ *outError = [self error:kJSONScannerErrorCode_CouldNotDecodeData description:@"Could not scan data. Data wasn't encoded properly?"];
}
return(NO);
}
@@ -205,6 +204,7 @@ - (BOOL)scanJSONObject:(id *)outObject error:(NSError **)outError
theResult = NO;
if (outError)
{
+ *outError = [self error:kJSONScannerErrorCode_CouldNotScanObject description:@"Could not scan object. Character not a valid JSON character."];
NSMutableDictionary *theUserInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"Could not scan object. Character not a valid JSON character.", NSLocalizedDescriptionKey,
NULL];
@@ -230,11 +230,7 @@ - (BOOL)scanJSONDictionary:(NSDictionary **)outDictionary error:(NSError **)outE
{
if (outError)
{
- NSMutableDictionary *theUserInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
- @"Could not scan dictionary. Dictionary that does not start with '{' character.", NSLocalizedDescriptionKey,
- NULL];
- [theUserInfo addEntriesFromDictionary:self.userInfoForScanLocation];
- *outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:kJSONScannerErrorCode_DictionaryStartCharacterMissing userInfo:theUserInfo];
+ *outError = [self error:kJSONScannerErrorCode_DictionaryStartCharacterMissing description:@"Could not scan dictionary. Dictionary that does not start with '{' character."];
}
return(NO);
}
@@ -254,11 +250,7 @@ - (BOOL)scanJSONDictionary:(NSDictionary **)outDictionary error:(NSError **)outE
[self setScanLocation:theScanLocation];
if (outError)
{
- NSMutableDictionary *theUserInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
- @"Could not scan dictionary. Failed to scan a key.", NSLocalizedDescriptionKey,
- NULL];
- [theUserInfo addEntriesFromDictionary:self.userInfoForScanLocation];
- *outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:kJSONScannerErrorCode_DictionaryKeyScanFailed userInfo:theUserInfo];
+ *outError = [self error:kJSONScannerErrorCode_DictionaryKeyScanFailed description:@"Could not scan dictionary. Failed to scan a key."];
}
[theDictionary release];
return(NO);
@@ -271,11 +263,7 @@ - (BOOL)scanJSONDictionary:(NSDictionary **)outDictionary error:(NSError **)outE
[self setScanLocation:theScanLocation];
if (outError)
{
- NSMutableDictionary *theUserInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
- @"Could not scan dictionary. Key was not terminated with a ':' character.", NSLocalizedDescriptionKey,
- NULL];
- [theUserInfo addEntriesFromDictionary:self.userInfoForScanLocation];
- *outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:kJSONScannerErrorCode_DictionaryKeyNotTerminated userInfo:theUserInfo];
+ *outError = [self error:kJSONScannerErrorCode_DictionaryKeyNotTerminated description:@"Could not scan dictionary. Key was not terminated with a ':' character."];
}
[theDictionary release];
return(NO);
@@ -287,12 +275,7 @@ - (BOOL)scanJSONDictionary:(NSDictionary **)outDictionary error:(NSError **)outE
[self setScanLocation:theScanLocation];
if (outError)
{
- NSMutableDictionary *theUserInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
- @"Could not scan dictionary. Failed to scan a value.", NSLocalizedDescriptionKey,
- NULL];
-
- [theUserInfo addEntriesFromDictionary:self.userInfoForScanLocation];
- *outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:kJSONScannerErrorCode_DictionaryValueScanFailed userInfo:theUserInfo];
+ *outError = [self error:kJSONScannerErrorCode_DictionaryValueScanFailed description:@"Could not scan dictionary. Failed to scan a value."];
}
[theDictionary release];
return(NO);
@@ -315,11 +298,7 @@ - (BOOL)scanJSONDictionary:(NSDictionary **)outDictionary error:(NSError **)outE
[self setScanLocation:theScanLocation];
if (outError)
{
- NSMutableDictionary *theUserInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
- @"Could not scan dictionary. Key value pairs not delimited with a ',' character.", NSLocalizedDescriptionKey,
- NULL];
- [theUserInfo addEntriesFromDictionary:self.userInfoForScanLocation];
- *outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:kJSONScannerErrorCode_DictionaryKeyValuePairNoDelimiter userInfo:theUserInfo];
+ *outError = [self error:kJSONScannerErrorDomain description:@"kJSONScannerErrorCode_DictionaryKeyValuePairNoDelimiter"];
@ntamas

ntamas Jul 24, 2011

This should probably be as follows:

*outError = [self error:kJSONScannerErrorCode_DictionaryKeyValuePairNoDelimiter description:@"....."];

@schwa

schwa Jul 27, 2011

Owner

Thanks. Fixing.

}
[theDictionary release];
return(NO);
@@ -339,11 +318,7 @@ - (BOOL)scanJSONDictionary:(NSDictionary **)outDictionary error:(NSError **)outE
[self setScanLocation:theScanLocation];
if (outError)
{
- NSMutableDictionary *theUserInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
- @"Could not scan dictionary. Dictionary not terminated by a '}' character.", NSLocalizedDescriptionKey,
- NULL];
- [theUserInfo addEntriesFromDictionary:self.userInfoForScanLocation];
- *outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:kJSONScannerErrorCode_DictionaryNotTerminated userInfo:theUserInfo];
+ *outError = [self error:kJSONScannerErrorCode_DictionaryNotTerminated description:@"Could not scan dictionary. Dictionary not terminated by a '}' character."];
}
[theDictionary release];
return(NO);
@@ -379,11 +354,7 @@ - (BOOL)scanJSONArray:(NSArray **)outArray error:(NSError **)outError
{
if (outError)
{
- NSMutableDictionary *theUserInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
- @"Could not scan array. Array not started by a '[' character.", NSLocalizedDescriptionKey,
- NULL];
- [theUserInfo addEntriesFromDictionary:self.userInfoForScanLocation];
- *outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:kJSONScannerErrorCode_ArrayStartCharacterMissing userInfo:theUserInfo];
+ *outError = [self error:kJSONScannerErrorCode_ArrayStartCharacterMissing description:@"Could not scan array. Array not started by a '[' character."];
}
return(NO);
}
@@ -399,6 +370,7 @@ - (BOOL)scanJSONArray:(NSArray **)outArray error:(NSError **)outError
[self setScanLocation:theScanLocation];
if (outError)
{
+ *outError = [self error:kJSONScannerErrorCode_ArrayValueScanFailed description:@"Could not scan array. Could not scan a value."];
NSMutableDictionary *theUserInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"Could not scan array. Could not scan a value.", NSLocalizedDescriptionKey,
NULL];
@@ -415,11 +387,7 @@ - (BOOL)scanJSONArray:(NSArray **)outArray error:(NSError **)outError
{
if (outError)
{
- NSMutableDictionary *theUserInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
- @"Could not scan array. Value is NULL.", NSLocalizedDescriptionKey,
- NULL];
- [theUserInfo addEntriesFromDictionary:self.userInfoForScanLocation];
- *outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:kJSONScannerErrorCode_ArrayValueIsNull userInfo:theUserInfo];
+ *outError = [self error:kJSONScannerErrorCode_ArrayValueIsNull description:@"Could not scan array. Value is NULL."];
}
[theArray release];
return(NO);
@@ -439,11 +407,7 @@ - (BOOL)scanJSONArray:(NSArray **)outArray error:(NSError **)outError
[self setScanLocation:theScanLocation];
if (outError)
{
- NSMutableDictionary *theUserInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
- @"Could not scan array. Array not terminated by a ']' character.", NSLocalizedDescriptionKey,
- NULL];
- [theUserInfo addEntriesFromDictionary:self.userInfoForScanLocation];
- *outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:kJSONScannerErrorCode_ArrayNotTerminated userInfo:theUserInfo];
+ *outError = [self error:kJSONScannerErrorCode_ArrayNotTerminated description:@"Could not scan array. Array not terminated by a ']' character."];
}
[theArray release];
return(NO);
@@ -461,11 +425,7 @@ - (BOOL)scanJSONArray:(NSArray **)outArray error:(NSError **)outError
[self setScanLocation:theScanLocation];
if (outError)
{
- NSMutableDictionary *theUserInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
- @"Could not scan array. Array not terminated by a ']' character.", NSLocalizedDescriptionKey,
- NULL];
- [theUserInfo addEntriesFromDictionary:self.userInfoForScanLocation];
- *outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:kJSONScannerErrorCode_ArrayNotTerminated userInfo:theUserInfo];
+ *outError = [self error:kJSONScannerErrorCode_ArrayNotTerminated description:@"Could not scan array. Array not terminated by a ']' character."];
}
[theArray release];
return(NO);
@@ -503,11 +463,7 @@ - (BOOL)scanJSONStringConstant:(NSString **)outStringConstant error:(NSError **)
[self setScanLocation:theScanLocation];
if (outError)
{
- NSMutableDictionary *theUserInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
- @"Could not scan string constant. String not started by a '\"' character.", NSLocalizedDescriptionKey,
- NULL];
- [theUserInfo addEntriesFromDictionary:self.userInfoForScanLocation];
- *outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:kJSONScannerErrorCode_StringNotStartedWithBackslash userInfo:theUserInfo];
+ *outError = [self error:kJSONScannerErrorCode_StringNotStartedWithBackslash description:@"Could not scan string constant. String not started by a '\"' character."];
}
[theString release];
return(NO);
@@ -557,11 +513,7 @@ - (BOOL)scanJSONStringConstant:(NSString **)outStringConstant error:(NSError **)
[self setScanLocation:theScanLocation];
if (outError)
{
- NSMutableDictionary *theUserInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
- @"Could not scan string constant. Unicode character could not be decoded.", NSLocalizedDescriptionKey,
- NULL];
- [theUserInfo addEntriesFromDictionary:self.userInfoForScanLocation];
- *outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:kJSONScannerErrorCode_StringUnicodeNotDecoded userInfo:theUserInfo];
+ *outError = [self error:kJSONScannerErrorCode_StringUnicodeNotDecoded description:@"Could not scan string constant. Unicode character could not be decoded."];
}
[theString release];
return(NO);
@@ -577,11 +529,7 @@ - (BOOL)scanJSONStringConstant:(NSString **)outStringConstant error:(NSError **)
[self setScanLocation:theScanLocation];
if (outError)
{
- NSMutableDictionary *theUserInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
- @"Could not scan string constant. Unknown escape code.", NSLocalizedDescriptionKey,
- NULL];
- [theUserInfo addEntriesFromDictionary:self.userInfoForScanLocation];
- *outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:kJSONScannerErrorCode_StringUnknownEscapeCode userInfo:theUserInfo];
+ *outError = [self error:kJSONScannerErrorCode_StringUnknownEscapeCode description:@"Could not scan string constant. Unknown escape code."];
}
[theString release];
return(NO);
@@ -595,11 +543,7 @@ - (BOOL)scanJSONStringConstant:(NSString **)outStringConstant error:(NSError **)
{
if (outError)
{
- NSMutableDictionary *theUserInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
- @"Could not scan string constant. No terminating double quote character.", NSLocalizedDescriptionKey,
- NULL];
- [theUserInfo addEntriesFromDictionary:self.userInfoForScanLocation];
- *outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:kJSONScannerErrorCode_StringNotTerminated userInfo:theUserInfo];
+ *outError = [self error:kJSONScannerErrorCode_StringNotTerminated description:@"Could not scan string constant. No terminating double quote character."];
}
[theString release];
return(NO);
@@ -642,11 +586,7 @@ - (BOOL)scanJSONNumberConstant:(NSNumber **)outNumberConstant error:(NSError **)
{
if (outError)
{
- NSMutableDictionary *theUserInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
- @"Could not scan number constant.", NSLocalizedDescriptionKey,
- NULL];
- [theUserInfo addEntriesFromDictionary:self.userInfoForScanLocation];
- *outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:kJSONScannerErrorCode_NumberNotScannable userInfo:theUserInfo];
+ *outError = [self error:kJSONScannerErrorCode_NumberNotScannable description:@"Could not scan number constant."];
}
return(NO);
}
@@ -685,4 +625,17 @@ - (BOOL)scanNotQuoteCharactersIntoString:(NSString **)outValue
return(YES);
}
+#pragma mark -
+
+- (NSError *)error:(NSInteger)inCode description:(NSString *)inDescription
+ {
+ NSParameterAssert(inDescription != NULL);
+ NSMutableDictionary *theUserInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
+ inDescription, NSLocalizedDescriptionKey,
+ NULL];
+ [theUserInfo addEntriesFromDictionary:self.userInfoForScanLocation];
+ NSError *theError = [NSError errorWithDomain:kJSONScannerErrorDomain code:inCode userInfo:theUserInfo];
+ return(theError);
+ }
+
@end

0 comments on commit 6416929

Please sign in to comment.