Skip to content

Return spec-compliant JSON-RPC error response on panic in WithSentry middleware - #19

Open
EllaSedova wants to merge 1 commit into
vmkteam:masterfrom
EllaSedova:master
Open

Return spec-compliant JSON-RPC error response on panic in WithSentry middleware#19
EllaSedova wants to merge 1 commit into
vmkteam:masterfrom
EllaSedova:master

Conversation

@EllaSedova

@EllaSedova EllaSedova commented Apr 2, 2026

Copy link
Copy Markdown

Problem

When a panic occurs inside an RPC handler wrapped by WithSentry, the defer/recover block catches the panic and reports it to Sentry, but the response returned to the client is an empty zenrpc.Response zero-value:

{
   "jsonrpc": "", 
    "id": 1
} 

This violates the https://www.jsonrpc.org/specification,

  • "jsonrpc" field to always be "2.0"
  • An "error" object to be present when the call fails

Fix

Two minimal changes in WithSentry:

  1. Named return value - changed the anonymous return zenrpc.Response to a named return (resp zenrpc.Response) so that the deferred recovery function can assign a proper response.
  2. Set error response on panic - added resp = zenrpc.NewResponseError(nil, zenrpc.InternalError, "", nil) inside the recover block, which produces a spec-compliant error response:
{
    "jsonrpc": "2.0", 
    "id": 1, 
    "error": 
    {
        "code": -32603, 
        "message": "Internal error"
    }
}

Error code -32603 (InternalError) is the appropriate code defined by the JSON-RPC 2.0 spec for internal server errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant