Skip to content

Commit

Permalink
feat: improve grpc exception descriptions
Browse files Browse the repository at this point in the history
add stacktrace and exception class to GRPC exception
  • Loading branch information
justenwalker committed Apr 24, 2021
1 parent ff529ae commit 7c0b5f0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void runContainer(DockerContainerSpec request, StreamObserver<DockerConta
responseObserver.onCompleted();
} catch (Exception e) {
log.error("GrpcService: runContainer error", e);
responseObserver.onError(e);
responseObserver.onError(GrpcUtils.toStatusException(e));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import io.grpc.StatusRuntimeException;
import tech.justen.concord.goodwill.grpc.ContextProto.*;

import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.*;

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -184,7 +187,7 @@ private static Any getAny(List<Object> value) {
}

public static StatusRuntimeException toStatusException(Exception ex) {
return Status.INTERNAL.withDescription(ex.getMessage()).withCause(ex).asRuntimeException();
return Status.INTERNAL.withDescription(exceptionDescription(ex)).withCause(ex).asRuntimeException();
}

public static StatusRuntimeException toStatusException(ApiException ex, String desc) {
Expand All @@ -210,4 +213,16 @@ public static StatusRuntimeException toStatusException(ApiException ex, String d
}
return status.withDescription(ex.getMessage()).withCause(ex).asRuntimeException();
}

private static String exceptionDescription(Throwable ex) {
StringWriter sw = new StringWriter();
sw.append(ex.getClass().getName());
if (ex.getMessage() != null) {
sw.append(": ");
sw.append(ex.getMessage());
sw.append('\n');
}
ex.printStackTrace(new PrintWriter(sw));
return sw.toString();
}
}

0 comments on commit 7c0b5f0

Please sign in to comment.