Remove the double bang operator and replace it with a null check instead. (#49)

2. add try catch in the block for transport send in acatch block

Co-authored-by: Leonid Stashevsky <e5l@users.noreply.github.com>
This commit is contained in:
Adithya Kamath 2025-03-12 15:18:21 +05:30 • committed by GitHub
commit 7d2e993b96
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -209,7 +209,7 @@ public abstract class Protocol(
if (handler === null) { if (handler === null) {
LOGGER.trace { "No handler found for request: ${request.method}" } LOGGER.trace { "No handler found for request: ${request.method}" }
try { try {
transport!!.send( transport?.send(
JSONRPCResponse( JSONRPCResponse(
id = request.id, id = request.id,
error = JSONRPCError( error = JSONRPCError(
@ -229,24 +229,30 @@ public abstract class Protocol(
val result = handler(request, RequestHandlerExtra()) val result = handler(request, RequestHandlerExtra())
LOGGER.trace { "Request handled successfully: ${request.method} (id: ${request.id})" } LOGGER.trace { "Request handled successfully: ${request.method} (id: ${request.id})" }
val response = JSONRPCResponse( transport?.send(
id = request.id, JSONRPCResponse(
result = result id = request.id,
result = result
)
) )
transport!!.send(response)
} catch (cause: Throwable) { } catch (cause: Throwable) {
LOGGER.error(cause) { "Error handling request: ${request.method} (id: ${request.id})" } LOGGER.error(cause) { "Error handling request: ${request.method} (id: ${request.id})" }
transport!!.send( try {
JSONRPCResponse( transport?.send(
id = request.id, JSONRPCResponse(
error = JSONRPCError( id = request.id,
ErrorCode.Defined.InternalError, error = JSONRPCError(
message = cause.message ?: "Internal error", code = ErrorCode.Defined.InternalError,
message = cause.message ?: "Internal error"
)
) )
) )
) } catch (sendError: Throwable) {
LOGGER.error(sendError) { "Failed to send error response for request: ${request.method} (id: ${request.id})" }
// Optionally implement fallback behavior here
}
} }
} }
@ -385,7 +391,7 @@ public abstract class Protocol(
try { try {
withTimeout(timeout) { withTimeout(timeout) {
LOGGER.trace { "Sending request message with id: $messageId" } LOGGER.trace { "Sending request message with id: $messageId" }
this@Protocol.transport!!.send(message) this@Protocol.transport?.send(message)
} }
return result.await() return result.await()
} catch (cause: TimeoutCancellationException) { } catch (cause: TimeoutCancellationException) {