Skip to content

Commit 2955cb7

Browse files
Preserve stacktrace order for unknown exceptions
1 parent 9a1d031 commit 2955cb7

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

stub/src/main/java/io/grpc/kotlin/ServerCalls.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,9 @@ object ServerCalls {
262262
null -> Status.OK
263263
is CancellationException -> Status.CANCELLED.withCause(failure)
264264
is StatusException, is StatusRuntimeException -> Status.fromThrowable(failure)
265-
else -> Status.fromThrowable(failure).withCause(failure)
265+
else -> Status.UNKNOWN
266+
.withDescription("Unknown Exception")
267+
.withCause(failure)
266268
}
267269
val trailers = failure?.let { Status.trailersFromThrowable(it) } ?: GrpcMetadata()
268270
mutex.withLock { call.close(closeStatus, trailers) }

stub/src/test/java/io/grpc/kotlin/ServerCallsTest.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,7 +1184,7 @@ class ServerCallsTest : AbstractCallsTest() {
11841184
}
11851185
}
11861186

1187-
val receivedStatusCause = CompletableDeferred<Throwable?>()
1187+
val receivedStatus = CompletableDeferred<Status?>()
11881188

11891189
val interceptor = object : ServerInterceptor {
11901190
override fun <ReqT, RespT> interceptCall(
@@ -1195,7 +1195,7 @@ class ServerCallsTest : AbstractCallsTest() {
11951195
next.startCall(
11961196
object : ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(call) {
11971197
override fun close(status: Status, trailers: Metadata) {
1198-
receivedStatusCause.complete(status.cause)
1198+
receivedStatus.complete(status)
11991199
super.close(status, trailers)
12001200
}
12011201
},
@@ -1213,11 +1213,13 @@ class ServerCallsTest : AbstractCallsTest() {
12131213
// the exception should not propagate to the client
12141214
assertThat(clientException.cause).isNull()
12151215

1216-
assertThat(clientException.status.code).isEqualTo(Status.Code.INTERNAL)
1217-
val statusCause = receivedStatusCause.await()
1216+
assertThat(clientException.status.code).isEqualTo(Status.Code.UNKNOWN)
1217+
val status = receivedStatus.await()!!
12181218
// but the exception should propagate to server interceptors, with stack trace intact
1219-
assertThat(statusCause).isEqualTo(thrownStatusCause.await())
1220-
assertThat(statusCause!!.stackTraceToString()).contains("internalServerCall")
1219+
assertThat(status.code).isEqualTo(Status.UNKNOWN.code)
1220+
assertThat(status.description).isEqualTo("Unknown Exception")
1221+
assertThat(status.cause).isEqualTo(thrownStatusCause.await())
1222+
assertThat(status.cause?.stackTraceToString()).contains("internalServerCall")
12211223
}
12221224

12231225
}

0 commit comments

Comments
 (0)