Decode gRPC error message (#326)

This commit is contained in:
Koichi Ishida 2020-07-20 23:11:19 +09:00 committed by GitHub
parent 27d9164f28
commit 98ff843751
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 2 deletions

View File

@ -242,7 +242,9 @@ class ClientCall<Q, R> implements Response {
// TODO(jakobr): Parse more!
if (metadata.containsKey('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) {
_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'.
final statusCode = status != null ? int.parse(status) : 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));
}
}

View File

@ -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 {
void handleRequest(_) {
harness.toClient.addError('Test error');