Skip to content

Commit

Permalink
Replace StatusCode implementation with auto-generated equivalent from…
Browse files Browse the repository at this point in the history
… code.proto. Includes status code string name in the error now
  • Loading branch information
acoutts committed Sep 14, 2020
1 parent df0ba21 commit e6049a4
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 187 deletions.
16 changes: 9 additions & 7 deletions interop/lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import 'package:grpc/grpc.dart';
import 'generated/empty.pb.dart';
import 'generated/messages.pb.dart';
import 'generated/test.pbgrpc.dart';
import 'package:grpc/src/generated/google/rpc/code.pbenum.dart';

const _headerEchoKey = 'x-grpc-test-echo-initial';
const _headerEchoData = 'test_initial_metadata_value';
Expand Down Expand Up @@ -1000,9 +1001,10 @@ class Tester {
/// * received status message is the same as the sent message for both
/// Procedure steps 1 and 2
Future<void> statusCodeAndMessage() async {
final expectedStatus = GrpcError.custom(2, 'test status message');
final expectedStatus =
GrpcError.custom(Code.UNKNOWN, 'test status message');
final responseStatus = EchoStatus()
..code = expectedStatus.code
..code = expectedStatus.code.value
..message = expectedStatus.message;
try {
await client.unaryCall(SimpleRequest()..responseStatus = responseStatus);
Expand Down Expand Up @@ -1044,7 +1046,7 @@ class Tester {
await client.unimplementedCall(Empty());
throw 'Did not throw.';
} on GrpcError catch (e) {
if (e.code != StatusCode.unimplemented) {
if (e.code != Code.UNIMPLEMENTED) {
throw 'Unexpected status code ${e.code} - ${e.message}.';
}
}
Expand All @@ -1064,7 +1066,7 @@ class Tester {
await unimplementedServiceClient.unimplementedCall(Empty());
throw 'Did not throw.';
} on GrpcError catch (e) {
if (e.code != StatusCode.unimplemented) {
if (e.code != Code.UNIMPLEMENTED) {
throw 'Unexpected status code ${e.code} - ${e.message}.';
}
}
Expand All @@ -1087,7 +1089,7 @@ class Tester {
await call;
throw 'Expected exception.';
} on GrpcError catch (e) {
if (e.code != StatusCode.cancelled) {
if (e.code != Code.CANCELLED) {
throw 'Unexpected status code ${e.code} - ${e.message}';
}
}
Expand Down Expand Up @@ -1131,7 +1133,7 @@ class Tester {
call.cancel();
}, onError: (e) {
if (e is! GrpcError) completer.completeError('Unexpected error: $e.');
if (e.code != StatusCode.cancelled) {
if (e.code != Code.CANCELLED) {
completer
.completeError('Unexpected status code ${e.code}: ${e.message}.');
}
Expand Down Expand Up @@ -1175,7 +1177,7 @@ class Tester {
}
throw 'Expected exception.';
} on GrpcError catch (e) {
if (e.code != StatusCode.deadlineExceeded) {
if (e.code != Code.DEADLINE_EXCEEDED) {
throw 'Unexpected status code ${e.code} - ${e.message}.';
}
} finally {
Expand Down
5 changes: 2 additions & 3 deletions lib/grpc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ export 'src/client/options.dart'
ChannelOptions;
export 'src/client/transport/http2_credentials.dart'
show BadCertificateHandler, allowBadCertificates, ChannelCredentials;

/// Status detail types
export 'src/generated/google/rpc/code.pb.dart';
export 'src/generated/google/rpc/error_details.pb.dart';
export 'src/server/call.dart' show ServiceCall;
export 'src/server/interceptor.dart' show Interceptor;
Expand All @@ -48,6 +47,6 @@ export 'src/shared/message.dart'
show GrpcMessage, GrpcMetadata, GrpcData, grpcDecompressor;
export 'src/shared/security.dart'
show supportedAlpnProtocols, createSecurityContext;
export 'src/shared/status.dart' show StatusCode, GrpcError;
export 'src/shared/status.dart' show GrpcError;
export 'src/shared/streams.dart' show GrpcHttpEncoder, GrpcHttpDecoder;
export 'src/shared/timeout.dart' show toTimeoutString, fromTimeoutString;
37 changes: 16 additions & 21 deletions lib/src/client/call.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import 'dart:async';
import 'dart:convert';

import 'package:grpc/src/generated/google/rpc/code.pbenum.dart';
import 'package:grpc/src/generated/google/rpc/status.pb.dart';
import 'package:protobuf/protobuf.dart';

import '../shared/message.dart';
import '../shared/status.dart';
Expand Down Expand Up @@ -244,15 +244,14 @@ class ClientCall<Q, R> implements Response {
_trailers.complete(metadata);
// TODO(jakobr): Parse more!
if (metadata.containsKey('grpc-status')) {
final status = int.parse(metadata['grpc-status']);
final message = metadata['grpc-message'] == null
? null
: Uri.decodeFull(metadata['grpc-message']);
if (status != 0) {
final details =
_parseStatusDetails(metadata['grpc-status-details-bin']);
final statusCode = details.code;
if (statusCode != 0) {
_responseError(GrpcError.custom(
status,
message,
_decodeStatusDetails(metadata['grpc-status-details-bin']),
Code.values[statusCode],
details.message,
details.details.map((e) => parseGeneratedMessage(e)).toList(),
));
}
}
Expand Down Expand Up @@ -288,18 +287,14 @@ class ClientCall<Q, R> implements Response {
// Only received a header frame and no data frames, so the header
// should contain "trailers" as well (Trailers-Only).
_trailers.complete(_headerMetadata);
final status = _headerMetadata['grpc-status'];
// If status code is missing, we must treat it as '0'. As in 'success'.
final statusCode = status != null ? int.parse(status) : 0;

final details =
_parseStatusDetails(_headerMetadata['grpc-status-details-bin']);
final statusCode = details.code;
if (statusCode != 0) {
final message = _headerMetadata['grpc-message'] == null
? null
: Uri.decodeFull(_headerMetadata['grpc-message']);
_responseError(GrpcError.custom(
statusCode,
message,
_decodeStatusDetails(_headerMetadata['grpc-status-details-bin']),
Code.values[statusCode],
details.message,
details.details.map((e) => parseGeneratedMessage(e)).toList(),
));
}
}
Expand Down Expand Up @@ -365,7 +360,7 @@ class ClientCall<Q, R> implements Response {
}
}

List<GeneratedMessage> _decodeStatusDetails(String data) {
Status _parseStatusDetails(String data) {
/// Parse details out of message. Length must be an even multiple of 4 so we pad it if needed.
var details = data ?? '';
while (details.length % 4 != 0) {
Expand All @@ -374,5 +369,5 @@ List<GeneratedMessage> _decodeStatusDetails(String data) {

/// Parse each Any type into the correct GeneratedMessage
final parsedStatus = Status.fromBuffer(base64Url.decode(details));
return parsedStatus.details.map((e) => parseGeneratedMessage(e)).toList();
return parsedStatus;
}
2 changes: 1 addition & 1 deletion lib/src/server/handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ class ServerHandler_ extends ServiceCall {
}

void _sendError(GrpcError error) {
sendTrailers(status: error.code, message: error.message);
sendTrailers(status: error.code.value, message: error.message);
}

void cancel() {
Expand Down
Loading

0 comments on commit e6049a4

Please sign in to comment.