aboutsummaryrefslogtreecommitdiff
path: root/objectivec/GPBMessage.m
diff options
context:
space:
mode:
authorSergio Campamá <kaipi@google.com>2016-06-02 11:14:26 -0700
committerThomas Van Lenten <thomasvl@google.com>2016-06-02 14:14:26 -0400
commite34c09182ebe0ed2958aefa809159498923743d3 (patch)
tree2eaf97f34c4ece1c6391fb795b20acc2d46fa145 /objectivec/GPBMessage.m
parent0ab7a7f74458be56bfc65a71a46711637ee5962b (diff)
downloadprotobuf-e34c09182ebe0ed2958aefa809159498923743d3.tar.gz
protobuf-e34c09182ebe0ed2958aefa809159498923743d3.tar.bz2
protobuf-e34c09182ebe0ed2958aefa809159498923743d3.zip
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
Diffstat (limited to 'objectivec/GPBMessage.m')
-rw-r--r--objectivec/GPBMessage.m38
1 files changed, 26 insertions, 12 deletions
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