From e34c09182ebe0ed2958aefa809159498923743d3 Mon Sep 17 00:00:00 2001 From: Sergio Campamá Date: Thu, 2 Jun 2016 11:14:26 -0700 Subject: Improving the granularity parsing errors (#1623) Add more context to GPBCodedInputStream failures. Have GPBMessage parsing apis extract out the GPBCodedInputStream information and expose it. Update HeaderDocs with pointers to all error domains/codes. Expand the unittests to cover the full set of errors reported. Fixes https://github.com/google/protobuf/issues/1618 --- objectivec/GPBMessage.m | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) (limited to 'objectivec/GPBMessage.m') diff --git a/objectivec/GPBMessage.m b/objectivec/GPBMessage.m index f1cb3927..919fb931 100644 --- a/objectivec/GPBMessage.m +++ b/objectivec/GPBMessage.m @@ -53,6 +53,8 @@ NSString *const GPBMessageErrorDomain = GPBNSStringifySymbol(GPBMessageErrorDomain); +NSString *const GPBErrorReasonKey = @"Reason"; + static NSString *const kGPBDataCoderKey = @"GPBData"; // @@ -96,20 +98,35 @@ static NSMutableDictionary *CloneExtensionMap(NSDictionary *extensionMap, NSZone *zone) __attribute__((ns_returns_retained)); +#ifdef DEBUG static NSError *MessageError(NSInteger code, NSDictionary *userInfo) { return [NSError errorWithDomain:GPBMessageErrorDomain code:code userInfo:userInfo]; } +#endif -static NSError *MessageErrorWithReason(NSInteger code, NSString *reason) { - NSDictionary *userInfo = nil; - if ([reason length]) { - userInfo = @{ @"Reason" : reason }; +static NSError *ErrorFromException(NSException *exception) { + NSError *error = nil; + + if ([exception.name isEqual:GPBCodedInputStreamException]) { + NSDictionary *exceptionInfo = exception.userInfo; + error = exceptionInfo[GPBCodedInputStreamUnderlyingErrorKey]; } - return MessageError(code, userInfo); -} + if (!error) { + NSString *reason = exception.reason; + NSDictionary *userInfo = nil; + if ([reason length]) { + userInfo = @{ GPBErrorReasonKey : reason }; + } + + error = [NSError errorWithDomain:GPBMessageErrorDomain + code:GPBMessageErrorCodeOther + userInfo:userInfo]; + } + return error; +} static void CheckExtension(GPBMessage *self, GPBExtensionDescriptor *extension) { @@ -817,8 +834,7 @@ static GPBUnknownFieldSet *GetOrMakeUnknownFields(GPBMessage *self) { [self release]; self = nil; if (errorPtr) { - *errorPtr = MessageErrorWithReason(GPBMessageErrorCodeMalformedData, - exception.reason); + *errorPtr = ErrorFromException(exception); } } #ifdef DEBUG @@ -849,8 +865,7 @@ static GPBUnknownFieldSet *GetOrMakeUnknownFields(GPBMessage *self) { [self release]; self = nil; if (errorPtr) { - *errorPtr = MessageErrorWithReason(GPBMessageErrorCodeMalformedData, - exception.reason); + *errorPtr = ErrorFromException(exception); } } #ifdef DEBUG @@ -1923,8 +1938,7 @@ static GPBUnknownFieldSet *GetOrMakeUnknownFields(GPBMessage *self) { @catch (NSException *exception) { message = nil; if (errorPtr) { - *errorPtr = MessageErrorWithReason(GPBMessageErrorCodeMalformedData, - exception.reason); + *errorPtr = ErrorFromException(exception); } } #ifdef DEBUG -- cgit v1.2.3