Permalink
Browse files

Error codes, user info keys

  • Loading branch information...
gabriel committed Dec 11, 2009
1 parent a3b6564 commit 45d5beffc094e31fa7deac1bc451475cc2138c1a
Showing with 24 additions and 13 deletions.
  1. +7 −0 Classes/YAJLParser.h
  2. +17 −13 Classes/YAJLParser.m
View
@@ -35,12 +35,19 @@ extern NSString *const YAJLErrorDomain;
extern NSString *const YAJLParserException;
extern NSString *const YAJLParsingUnsupportedException;
+extern NSString *const YAJLParserValueKey; // Key in NSError userInfo for value we errored on
+
#ifdef DEBUG
#define YAJLDebug(...) NSLog(__VA_ARGS__)
#else
#define YAJLDebug(...) do {} while(0)
#endif
+enum {
+ YAJLParserErrorCodeAllocError = -1000,
+ YAJLParserErrorCodeDoubleOverflow = -1001,
+ YAJLParserErrorCodeIntegerOverflow = -1002
+} YAJLParserErrorCode;
/*!
@enum Parser options
View
@@ -34,6 +34,8 @@
NSString *const YAJLParserException = @"YAJLParserException";
NSString *const YAJLParsingUnsupportedException = @"YAJLParsingUnsupportedException";
+NSString *const YAJLParserValueKey = @"YAJLParserValueKey";
+
@interface YAJLParser ()
@property (retain, nonatomic) NSError *parserError;
@end
@@ -49,8 +51,8 @@ - (void)_endDictionary;
- (void)_startArray;
- (void)_endArray;
-- (NSError *)_errorForStatus:(NSInteger)code message:(NSString *)message;
-- (void)_cancelWithErrorForStatus:(NSInteger)code message:(NSString *)message;
+- (NSError *)_errorForStatus:(NSInteger)code message:(NSString *)message value:(NSString *)value;
+- (void)_cancelWithErrorForStatus:(NSInteger)code message:(NSString *)message value:(NSString *)value;
@end
@@ -81,13 +83,14 @@ - (void)dealloc {
#pragma mark Error Helpers
-- (NSError *)_errorForStatus:(NSInteger)code message:(NSString *)message {
- NSDictionary *userInfo = [NSDictionary dictionaryWithObject:message forKey:NSLocalizedDescriptionKey];
+- (NSError *)_errorForStatus:(NSInteger)code message:(NSString *)message value:(NSString *)value {
+ NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithObject:message forKey:NSLocalizedDescriptionKey];
+ if (value) [userInfo setObject:value forKey:YAJLParserValueKey];
return [NSError errorWithDomain:YAJLErrorDomain code:code userInfo:userInfo];
}
-- (void)_cancelWithErrorForStatus:(NSInteger)code message:(NSString *)message {
- self.parserError = [self _errorForStatus:code message:message];
+- (void)_cancelWithErrorForStatus:(NSInteger)code message:(NSString *)message value:(NSString *)value {
+ self.parserError = [self _errorForStatus:code message:message value:value];
}
#pragma mark YAJL Callbacks
@@ -105,8 +108,9 @@ int yajl_boolean(void *ctx, int boolVal) {
}
// Instead of using yajl_integer, and yajl_double we use yajl_number and parse
-// as double; This is to be more compliant since javascript numbers are represented
-// as double precision floating point
+// as double (or long long); This is to be more compliant since Javascript numbers are represented
+// as double precision floating point, though JSON spec doesn't define a max value
+// and is up to the parser?
//int yajl_integer(void *ctx, long integerVal) {
// [(id)ctx _add:[NSNumber numberWithLong:integerVal]];
@@ -127,7 +131,7 @@ int yajl_number(void *ctx, const char *numberVal, unsigned int numberLen) {
double d = strtod((char *)buf, NULL);
if ((d == HUGE_VAL || d == -HUGE_VAL) && errno == ERANGE) {
NSString *s = [[NSString alloc] initWithBytes:numberVal length:numberLen encoding:NSUTF8StringEncoding];
- [(id)ctx _cancelWithErrorForStatus:-2 message:[NSString stringWithFormat:@"double overflow on '%@'", s]];
+ [(id)ctx _cancelWithErrorForStatus:YAJLParserErrorCodeDoubleOverflow message:[NSString stringWithFormat:@"double overflow on '%@'", s] value:s];
[s release];
return 0;
}
@@ -139,7 +143,7 @@ int yajl_number(void *ctx, const char *numberVal, unsigned int numberLen) {
long long i = strtoll((const char *) buf, NULL, 10);
if ((i == LLONG_MIN || i == LLONG_MAX) && errno == ERANGE) {
NSString *s = [[NSString alloc] initWithBytes:numberVal length:numberLen encoding:NSUTF8StringEncoding];
- [(id)ctx _cancelWithErrorForStatus:-2 message:[NSString stringWithFormat:@"integer overflow on '%@'", s]];
+ [(id)ctx _cancelWithErrorForStatus:YAJLParserErrorCodeIntegerOverflow message:[NSString stringWithFormat:@"integer overflow on '%@'", s] value:s];
[s release];
return 0;
}
@@ -234,7 +238,7 @@ - (YAJLParserStatus)parse:(NSData *)data {
handle_ = yajl_alloc(&callbacks, &cfg, NULL, self);
if (!handle_) {
- self.parserError = [self _errorForStatus:-1 message:@"Unable to allocate YAJL handle"];
+ self.parserError = [self _errorForStatus:YAJLParserErrorCodeAllocError message:@"Unable to allocate YAJL handle" value:nil];
return YAJLParserStatusError;
}
}
@@ -248,15 +252,15 @@ - (YAJLParserStatus)parse:(NSData *)data {
} else if (status == yajl_status_error) {
unsigned char *errorMessage = yajl_get_error(handle_, 1, [data bytes], [data length]);
NSString *errorString = [NSString stringWithUTF8String:(char *)errorMessage];
- self.parserError = [self _errorForStatus:status message:errorString];
+ self.parserError = [self _errorForStatus:status message:errorString value:nil];
yajl_free_error(handle_, errorMessage);
return YAJLParserStatusError;
} else if (status == yajl_status_insufficient_data) {
return YAJLParserStatusInsufficientData;
} else if (status == yajl_status_ok) {
return YAJLParserStatusOK;
} else {
- self.parserError = [self _errorForStatus:status message:[NSString stringWithFormat:@"Unexpected status %d", status]];
+ self.parserError = [self _errorForStatus:status message:[NSString stringWithFormat:@"Unexpected status %d", status] value:nil];
return YAJLParserStatusError;
}
}

0 comments on commit 45d5bef

Please sign in to comment.