mirror of https://github.com/grpc/grpc-dart.git
				
				
				
			Decode gRPC error message (#326)
This commit is contained in:
		
							parent
							
								
									27d9164f28
								
							
						
					
					
						commit
						98ff843751
					
				|  | @ -242,7 +242,9 @@ class ClientCall<Q, R> implements Response { | ||||||
|       // TODO(jakobr): Parse more! |       // TODO(jakobr): Parse more! | ||||||
|       if (metadata.containsKey('grpc-status')) { |       if (metadata.containsKey('grpc-status')) { | ||||||
|         final status = int.parse(metadata['grpc-status']); |         final status = int.parse(metadata['grpc-status']); | ||||||
|         final message = metadata['grpc-message']; |         final message = metadata['grpc-message'] == null | ||||||
|  |             ? null | ||||||
|  |             : Uri.decodeFull(metadata['grpc-message']); | ||||||
|         if (status != 0) { |         if (status != 0) { | ||||||
|           _responseError(GrpcError.custom(status, message)); |           _responseError(GrpcError.custom(status, message)); | ||||||
|         } |         } | ||||||
|  | @ -283,7 +285,9 @@ class ClientCall<Q, R> implements Response { | ||||||
|       // If status code is missing, we must treat it as '0'. As in 'success'. |       // If status code is missing, we must treat it as '0'. As in 'success'. | ||||||
|       final statusCode = status != null ? int.parse(status) : 0; |       final statusCode = status != null ? int.parse(status) : 0; | ||||||
|       if (statusCode != 0) { |       if (statusCode != 0) { | ||||||
|         final message = _headerMetadata['grpc-message']; |         final message = _headerMetadata['grpc-message'] == null | ||||||
|  |             ? null | ||||||
|  |             : Uri.decodeFull(_headerMetadata['grpc-message']); | ||||||
|         _responseError(GrpcError.custom(statusCode, message)); |         _responseError(GrpcError.custom(statusCode, message)); | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  | @ -257,6 +257,27 @@ void main() { | ||||||
|     ); |     ); | ||||||
|   }); |   }); | ||||||
| 
 | 
 | ||||||
|  |   test('Call throws decoded message', () async { | ||||||
|  |     const customStatusCode = 17; | ||||||
|  |     const customStatusMessage = 'エラー'; | ||||||
|  |     const encodedCustomStatusMessage = '%E3%82%A8%E3%83%A9%E3%83%BC'; | ||||||
|  | 
 | ||||||
|  |     void handleRequest(_) { | ||||||
|  |       harness.toClient.add(HeadersStreamMessage([ | ||||||
|  |         Header.ascii('grpc-status', '$customStatusCode'), | ||||||
|  |         Header.ascii('grpc-message', encodedCustomStatusMessage) | ||||||
|  |       ], endStream: true)); | ||||||
|  |       harness.toClient.close(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     await harness.runFailureTest( | ||||||
|  |       clientCall: harness.client.unary(dummyValue), | ||||||
|  |       expectedException: | ||||||
|  |           GrpcError.custom(customStatusCode, customStatusMessage), | ||||||
|  |       serverHandlers: [handleRequest], | ||||||
|  |     ); | ||||||
|  |   }); | ||||||
|  | 
 | ||||||
|   test('Call throws on response stream errors', () async { |   test('Call throws on response stream errors', () async { | ||||||
|     void handleRequest(_) { |     void handleRequest(_) { | ||||||
|       harness.toClient.addError('Test error'); |       harness.toClient.addError('Test error'); | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue