Permit some gRPC excptions to be logged only as warning

A new handleExceptionAsWarning method logs warn(ex.msg) instead of
the full stack trace.
This commit is contained in:
ghubstan 2021-02-27 17:14:22 -03:00
parent e5291e9f45
commit e5a0a3998d
No known key found for this signature in database
GPG key ID: E35592D6800A861E

View file

@ -46,7 +46,9 @@ class GrpcExceptionHandler {
public GrpcExceptionHandler() {
}
public void handleException(Logger log, Throwable t, StreamObserver<?> responseObserver) {
public void handleException(Logger log,
Throwable t,
StreamObserver<?> responseObserver) {
// Log the core api error (this is last chance to do that), wrap it in a new
// gRPC StatusRuntimeException, then send it to the client in the gRPC response.
log.error("", t);
@ -55,6 +57,17 @@ class GrpcExceptionHandler {
throw grpcStatusRuntimeException;
}
public void handleExceptionAsWarning(Logger log,
String calledMethod,
Throwable t,
StreamObserver<?> responseObserver) {
// Just log a warning instead of an error with full stack trace.
log.warn(calledMethod + " -> " + t.getMessage());
var grpcStatusRuntimeException = wrapException(t);
responseObserver.onError(grpcStatusRuntimeException);
throw grpcStatusRuntimeException;
}
private StatusRuntimeException wrapException(Throwable t) {
// We want to be careful about what kinds of exception messages we send to the
// client. Expected core exceptions should be wrapped in an IllegalStateException