Skip to content

Commit

Permalink
Correctly propagate errors through WebSocket async call stack
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorbg committed Apr 19, 2021
1 parent 8f25033 commit c267cad
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 13 deletions.
4 changes: 2 additions & 2 deletions tnoodle-ui/src/main/components/Interceptor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class Interceptor extends Component<{}, InterceptorState> {

updateMessage = (data: any) => {
// Clear the message after some seconds
let message = data.message || data.statusText || JSON.stringify(data);
let stackTrace = data.stackTrace;
let message = data?.message || data?.statusText || JSON.stringify(data);
let stackTrace = data?.stackTrace ?? "";

setTimeout(() => {
// We only clear the message if the user did not click "Show more"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ object JobSchedulingHandler : RouteHandler {

private const val JOB_ID_PARAM = "jobId"

const val MARKER_ERROR_MESSAGE = "%%ERROR%%";
const val MARKER_ERROR_MESSAGE = "%%ERROR%%"

private suspend fun ApplicationCall.checkAndYieldJobId(): Int? {
val jobId = parameters[JOB_ID_PARAM]?.toIntOrNull() ?: -1
Expand Down Expand Up @@ -134,7 +134,7 @@ object JobSchedulingHandler : RouteHandler {
send(MARKER_ERROR_MESSAGE)
send(errorSerial)

close(CloseReason(CloseReason.Codes.INTERNAL_ERROR, ""))
close(CloseReason(CloseReason.Codes.INTERNAL_ERROR, MARKER_ERROR_MESSAGE))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package org.worldcubeassociation.tnoodle.server.webscrambles.routing.job
import io.ktor.application.ApplicationCall
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import io.ktor.http.cio.websocket.CloseReason
import io.ktor.http.cio.websocket.Frame
import io.ktor.http.cio.websocket.close
import io.ktor.websocket.WebSocketServerSession
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -51,12 +49,6 @@ abstract class LongRunningJob<T> : CoroutineScope {

suspend fun channel(request: T, socket: WebSocketServerSession): Pair<ContentType, ByteArray> {
val statusBackend = StatusBackend.Websocket(socket)

try {
return request.compute(statusBackend)
} catch (t: Throwable) {
socket.close(CloseReason(CloseReason.Codes.INTERNAL_ERROR, t.toString()))
throw t
}
return request.compute(statusBackend)
}
}

0 comments on commit c267cad

Please sign in to comment.