Improve error handling (#204)

* Improve error handling

1) Let GrpcError implement exception

As the user is (usually) expected to catch these they should implement
Exception.
There is a bigger clean-up in splitting off the GrpcError.internal as
that seems to be for invariant violations

2) Convert some thrown strings into exceptions.
This commit is contained in:
Sigurd Meldgaard 2019-07-03 09:26:45 +02:00 committed by GitHub
parent c305f0d685
commit 3aeafa77cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 8 deletions

View File

@ -1,3 +1,7 @@
## 2.0.3
* GrpcError now implements Exception to indicate it can be reasonably handled.
## 2.0.2 ## 2.0.2
* Fix computation of the audience given to metadata providers to include the scheme. * Fix computation of the audience given to metadata providers to include the scheme.

View File

@ -73,8 +73,7 @@ class Http2ClientConnection implements connection.ClientConnection {
var socket = await Socket.connect(host, port); var socket = await Socket.connect(host, port);
if (_state == ConnectionState.shutdown) { if (_state == ConnectionState.shutdown) {
socket.destroy(); socket.destroy();
// TODO(sigurdm): Throw something nicer... throw _ShutdownException();
throw 'Shutting down';
} }
final securityContext = credentials.securityContext; final securityContext = credentials.securityContext;
if (securityContext != null) { if (securityContext != null) {
@ -84,8 +83,7 @@ class Http2ClientConnection implements connection.ClientConnection {
onBadCertificate: _validateBadCertificate); onBadCertificate: _validateBadCertificate);
if (_state == ConnectionState.shutdown) { if (_state == ConnectionState.shutdown) {
socket.destroy(); socket.destroy();
// TODO(sigurdm): Throw something nicer... throw _ShutdownException();
throw 'Shutting down';
} }
} }
socket.done.then((_) => _handleSocketClosed()); socket.done.then((_) => _handleSocketClosed());
@ -275,3 +273,5 @@ class Http2ClientConnection implements connection.ClientConnection {
return validator(certificate, authority); return validator(certificate, authority);
} }
} }
class _ShutdownException implements Exception {}

View File

@ -41,7 +41,7 @@ class GrpcMessageSink extends Sink<GrpcMessage> {
@override @override
void add(GrpcMessage data) { void add(GrpcMessage data) {
if (message != null) { if (message != null) {
throw 'Too many messages received!'; throw StateError('Too many messages received!');
} }
message = data; message = data;
} }
@ -49,7 +49,7 @@ class GrpcMessageSink extends Sink<GrpcMessage> {
@override @override
void close() { void close() {
if (message == null) { if (message == null) {
throw 'No messages received!'; throw StateError('No messages received!');
} }
} }
} }

View File

@ -117,7 +117,7 @@ class StatusCode {
static const unauthenticated = 16; static const unauthenticated = 16;
} }
class GrpcError { class GrpcError implements Exception {
final int code; final int code;
final String message; final String message;
@ -214,6 +214,7 @@ class GrpcError {
/// Internal errors. Means some invariants expected by underlying system has /// Internal errors. Means some invariants expected by underlying system has
/// been broken. If you see one of these errors, something is very broken. /// been broken. If you see one of these errors, something is very broken.
// TODO(sigurdm): This should probably not be an [Exception].
GrpcError.internal([this.message]) : code = StatusCode.internal; GrpcError.internal([this.message]) : code = StatusCode.internal;
/// The service is currently unavailable. This is a most likely a transient /// The service is currently unavailable. This is a most likely a transient

View File

@ -1,7 +1,7 @@
name: grpc name: grpc
description: Dart implementation of gRPC, a high performance, open-source universal RPC framework. description: Dart implementation of gRPC, a high performance, open-source universal RPC framework.
version: 2.0.2 version: 2.0.3
author: Dart Team <misc@dartlang.org> author: Dart Team <misc@dartlang.org>
homepage: https://github.com/dart-lang/grpc-dart homepage: https://github.com/dart-lang/grpc-dart