Skip to content

Enhance error handling in RPC functions for clearer messages#439

Merged
Marketen merged 2 commits intomainfrom
pablo/prettify-error-to-frontend
Apr 1, 2026
Merged

Enhance error handling in RPC functions for clearer messages#439
Marketen merged 2 commits intomainfrom
pablo/prettify-error-to-frontend

Conversation

@pablomendezroyo
Copy link
Copy Markdown
Contributor

@pablomendezroyo pablomendezroyo commented Mar 3, 2026

  • Add toRpcErrorMessage() helper on the backend to always produce a string error message
  • Add toErrorMessage() helper on the client to extract human-readable messages from JSON-RPC error responses
  • Fix JSON-RPC error codes: use -32601 specifically for method-not-found, and -32000 for all other caught server errors

@pablomendezroyo pablomendezroyo requested a review from a team as a code owner March 3, 2026 06:38
@Marketen
Copy link
Copy Markdown
Contributor

Marketen commented Apr 1, 2026

@copilot review this PR

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 1, 2026

@copilot review this PR

Here's the review of the PR changes (commit 253b2f6):

packages/brain/src/modules/apiServers/ui/startUiServer.ts

Added toRpcErrorMessage(error: unknown): string on the backend. This ensures the JSON-RPC 2.0 message field is always a string (the spec requires it). It correctly handles null/undefined, plain strings, Error instances, and arbitrary objects via JSON.stringify with a String() fallback.

One pre-existing note (not introduced by this PR): the error code -32601 is the JSON-RPC 2.0 "Method Not Found" code but is used here for all caught errors. The correct code for generic server errors is -32000 to -32099. This was in the code before these changes, but worth tracking as a follow-up.

packages/ui/src/socket/socket.ts

Added toErrorMessage(value: unknown): string on the client. It handles the JSON-RPC { code, message } shape and also the legacy case where the backend sent a raw error object as message (before this fix). The raw error payload is preserved on the thrown Error as .rpcError which is handy for debugging.

CI: ✅ Build and tests passed (22611466384).

Overall the changes are correct, well-scoped, and improve spec-compliance. The only suggestion for a follow-up is correcting the error code from -32601 to a server-error range code (e.g. -32000) for non-"method not found" failures.

Copilot AI requested a review from Marketen April 1, 2026 09:35
@Marketen
Copy link
Copy Markdown
Contributor

Marketen commented Apr 1, 2026

@copilot correct the error code from -32601 to a server-error range code (e.g. -32000) for non-"method not found" failures.

…32000 for server errors)

Agent-Logs-Url: https://github.com/dappnode/StakingBrain/sessions/1094ca8d-5049-4c30-8350-bd459cfdb87d

Co-authored-by: Marketen <36164126+Marketen@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 1, 2026

@copilot correct the error code from -32601 to a server-error range code (e.g. -32000) for non-"method not found" failures.

Done in 4a7bc07. The method-not-found check is now separated from the try/catch so each path uses the correct code:

  • -32601 when the requested method doesn't exist in rpcMethods
  • -32000 for any other runtime error caught during method execution

@Marketen Marketen merged commit 4b73069 into main Apr 1, 2026
2 checks passed
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.

3 participants