-
Notifications
You must be signed in to change notification settings - Fork 25
chore: replace hardcoded error codes with rpc error constants #376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
chaitanyapotti
approved these changes
Feb 11, 2026
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation and Context
The JRPC layer had several issues: loosely-typed interfaces allowed invalid param shapes to pass through without compile-time checks, error serialization could silently drop the original error message, hardcoded error codes were scattered across the codebase, and engine streams did not clean up event listeners on close—causing potential memory leaks.
Jira Link: https://consensyssoftware.atlassian.net/browse/W3APD-5273?atlOrigin=eyJpIjoiOWMxNTliMjI3ZGFjNGRmZjk0MTcyZDkzMjAwODAxZDMiLCJwIjoiaiJ9
Description
Type Safety Improvements
JRPCParamstype (Json[] | Record<string, Json>) and constrained all generic parameters onJRPCRequest,JRPCMiddleware,AsyncJRPCMiddleware, andExtendedJsonRpcRequestto extendJRPCParams, enforcing JSON-RPC-compliant param shapes at compile time.JRPCNotificationinterface to properly distinguish notifications (noid) from requests.New Utility Functions
isJRPCNotification,isJRPCRequest,isJRPCSuccess,isJRPCFailure,isValidMethod.isObject,hasProperty,isValidNumber.isObjectfromjrpc/errors/utils.tstoutils/utils.tsfor broader reuse.Error Handling Improvements
errorCodes.rpc.*constants throughout.shouldPreserveMessageoption toserializeJrpcError(defaults totrue) so original error messages are preserved in the serialized output instead of being replaced by the fallback.JRPCEngine._handleto include the actual received type (e.g.,"Must specify a string method. Received: undefined").constructFallbackErrorto validatecodewithisValidNumberinstead of blindly parsing, avoidingNaNcodes.Engine & Stream Fixes
_handleBatch—returns aninvalidRequesterror instead of silently returning an empty array.notificationevent listener when the engine stream is closed, preventing memory leaks.!res.idnotification check with theisJRPCNotificationtype guard.How has this been tested?
jrpcEngine.test.tscovering: single request handling, batch requests (including empty batch), error propagation, async middleware,asMiddleware(),mergeMiddleware, and engine stream creation/cleanup.jrpcMiddleware.test.tscovering:createErrorMiddleware,createStreamMiddleware,createScaffoldMiddleware,createIdRemapMiddleware,createLoggerMiddleware, andcreateAsyncMiddleware.jrpcUtils.test.tswith 106 new lines covering:isJRPCNotification,isJRPCRequest,isValidMethod,isJRPCSuccess,isJRPCFailure,isValidNumber,isObject, andhasProperty.Screenshots (if appropriate):
N/A
Types of changes
Checklist:
Note
Medium Risk
Touches core JSON-RPC engine/middleware and error serialization behavior, which can subtly change error messages/codes and batch/notification handling. Risk is mitigated by extensive new tests covering the updated edge cases.
Overview
Standardizes the JRPC layer around shared
errorCodes.rpc.*constants and stronger runtime validation, including improved request validation errors and safer fallback error construction (avoidsNaNcodes).Tightens typings by introducing
JRPCParamsandJRPCNotification, constraining middleware/request generics, and addingsrc/utils/jrpc.tstype guards (isJRPCNotification,isJRPCRequest,isJRPCSuccess/isJRPCFailure,isValidMethod) used throughout the engine/stream middleware.Improves error serialization to optionally preserve original error messages (
shouldPreserveMessage, default on) while still attaching a JSON-safedata.cause, handles empty batch requests asinvalidRequest, and fixes an engine stream notification listener leak by removing the listener on stream close. Adds new Vitest coverage for engine behavior, middleware helpers, and the new guards.Written by Cursor Bugbot for commit 3bef878. This will update automatically on new commits. Configure here.