Fix crash in grpc_errorFromStatusCode

pull/16696/head
Muxi Yan 6 years ago
parent ccbad108e4
commit f83ca91702
  1. 2
      src/objective-c/GRPCClient/private/NSError+GRPC.h
  2. 16
      src/objective-c/GRPCClient/private/NSError+GRPC.m

@ -25,6 +25,6 @@
* and whose domain is |kGRPCErrorDomain|. * and whose domain is |kGRPCErrorDomain|.
*/ */
+ (instancetype)grpc_errorFromStatusCode:(grpc_status_code)statusCode + (instancetype)grpc_errorFromStatusCode:(grpc_status_code)statusCode
details:(char *)details details:(const char *)details
errorString:(const char *)errorString; errorString:(const char *)errorString;
@end @end

@ -24,18 +24,20 @@ NSString *const kGRPCErrorDomain = @"io.grpc";
@implementation NSError (GRPC) @implementation NSError (GRPC)
+ (instancetype)grpc_errorFromStatusCode:(grpc_status_code)statusCode + (instancetype)grpc_errorFromStatusCode:(grpc_status_code)statusCode
details:(char *)details details:(const char *)details
errorString:(const char *)errorString { errorString:(const char *)errorString {
if (statusCode == GRPC_STATUS_OK) { if (statusCode == GRPC_STATUS_OK) {
return nil; return nil;
} }
NSString *message = [NSString stringWithCString:details encoding:NSUTF8StringEncoding]; NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
NSString *debugMessage = [NSString stringWithCString:errorString encoding:NSUTF8StringEncoding]; if (details) {
userInfo[NSLocalizedDescriptionKey] = [NSString stringWithCString:details encoding:NSUTF8StringEncoding];
}
if (errorString) {
userInfo[NSDebugDescriptionErrorKey] = [NSString stringWithCString:errorString encoding:NSUTF8StringEncoding];
}
return [NSError errorWithDomain:kGRPCErrorDomain return [NSError errorWithDomain:kGRPCErrorDomain
code:statusCode code:statusCode
userInfo:@{ userInfo:userInfo];
NSLocalizedDescriptionKey : message,
NSDebugDescriptionErrorKey : debugMessage
}];
} }
@end @end

Loading…
Cancel
Save