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

View File

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

View File

@ -117,7 +117,7 @@ class StatusCode {
static const unauthenticated = 16;
}
class GrpcError {
class GrpcError implements Exception {
final int code;
final String message;
@ -214,6 +214,7 @@ class GrpcError {
/// Internal errors. Means some invariants expected by underlying system has
/// 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;
/// The service is currently unavailable. This is a most likely a transient

View File

@ -1,7 +1,7 @@
name: grpc
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>
homepage: https://github.com/dart-lang/grpc-dart