Skip to content

Grpc Error Transformer Added #787

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/src/server/handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import 'service.dart';

typedef ServiceLookup = Service? Function(String service);
typedef GrpcErrorHandler = void Function(GrpcError error, StackTrace? trace);
typedef GrpcErrorTransformer = GrpcError? Function(GrpcError error, StackTrace? trace);

/// Handles an incoming gRPC call.
class ServerHandler extends ServiceCall {
Expand All @@ -40,6 +41,7 @@ class ServerHandler extends ServiceCall {
final List<ServerInterceptor> _serverInterceptors;
final CodecRegistry? _codecRegistry;
final GrpcErrorHandler? _errorHandler;
final GrpcErrorTransformer? _errorTransformer;

// ignore: cancel_subscriptions
StreamSubscription<GrpcMessage>? _incomingSubscription;
Expand Down Expand Up @@ -89,6 +91,7 @@ class ServerHandler extends ServiceCall {
X509Certificate? clientCertificate,
InternetAddress? remoteAddress,
GrpcErrorHandler? errorHandler,
GrpcErrorTransformer? errorTransformer,
this.onDataReceived,
}) : _stream = stream,
_serviceLookup = serviceLookup,
Expand All @@ -97,6 +100,7 @@ class ServerHandler extends ServiceCall {
_clientCertificate = clientCertificate,
_remoteAddress = remoteAddress,
_errorHandler = errorHandler,
_errorTransformer = errorTransformer,
_serverInterceptors = serverInterceptors;

@override
Expand Down Expand Up @@ -458,6 +462,7 @@ class ServerHandler extends ServiceCall {

void _sendError(GrpcError error, [StackTrace? trace]) {
_errorHandler?.call(error, trace);
error = _errorTransformer?.call(error, trace) ?? error;

sendTrailers(
status: error.code,
Expand Down
9 changes: 8 additions & 1 deletion lib/src/server/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class ConnectionServer {
final List<ServerInterceptor> _serverInterceptors;
final CodecRegistry? _codecRegistry;
final GrpcErrorHandler? _errorHandler;
final GrpcErrorTransformer? _errorTransformer;
final ServerKeepAliveOptions _keepAliveOptions;

@visibleForTesting
Expand All @@ -104,11 +105,13 @@ class ConnectionServer {
List<ServerInterceptor> serverInterceptors = const <ServerInterceptor>[],
CodecRegistry? codecRegistry,
GrpcErrorHandler? errorHandler,
GrpcErrorTransformer? errorTransformer,
this._keepAliveOptions = const ServerKeepAliveOptions(),
]) : _codecRegistry = codecRegistry,
_interceptors = interceptors,
_serverInterceptors = serverInterceptors,
_errorHandler = errorHandler {
_errorHandler = errorHandler,
_errorTransformer = errorTransformer {
for (final service in services) {
_services[service.$name] = service;
}
Expand Down Expand Up @@ -197,6 +200,7 @@ class Server extends ConnectionServer {
super.interceptors,
super.codecRegistry,
super.errorHandler,
super.errorTransformer,
super.keepAlive,
]);

Expand All @@ -208,12 +212,14 @@ class Server extends ConnectionServer {
List<ServerInterceptor> serverInterceptors = const <ServerInterceptor>[],
CodecRegistry? codecRegistry,
GrpcErrorHandler? errorHandler,
GrpcErrorTransformer? errorTransformer,
}) : super(
services,
interceptors,
serverInterceptors,
codecRegistry,
errorHandler,
errorTransformer,
keepAliveOptions,
);

Expand Down Expand Up @@ -321,6 +327,7 @@ class Server extends ConnectionServer {
// ignore: unnecessary_cast
remoteAddress: remoteAddress as io_bits.InternetAddress?,
errorHandler: _errorHandler,
errorTransformer: _errorTransformer,
onDataReceived: onDataReceived,
)..handle();
}
Expand Down