-
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
Open
lwin-kyaw
wants to merge
3
commits into
master
Choose a base branch
from
fix/jrpc-errors
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,19 @@ | |
|
|
||
| type ErrorValueKey = keyof typeof errorValues; | ||
|
|
||
| export function isValidNumber(value: unknown): value is number { | ||
| try { | ||
| if (typeof value === "number" && Number.isInteger(value)) { | ||
| return true; | ||
| } | ||
|
|
||
| const parsedValue = Number(value.toString()); | ||
| return Number.isInteger(parsedValue); | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns whether the given code is valid. | ||
| * A code is valid if it is an integer. | ||
|
|
@@ -210,9 +223,9 @@ | |
| * | ||
| * @param error - The error to serialize. | ||
| * @param options - Options bag. | ||
| * @param options.fallbackError - The error to return if the given error is | ||
|
Check warning on line 226 in src/jrpc/errors/utils.ts
|
||
| * not compatible. Should be a JSON serializable value. | ||
| * @param options.shouldIncludeStack - Whether to include the error's stack | ||
|
Check warning on line 228 in src/jrpc/errors/utils.ts
|
||
| * on the returned object. | ||
| * @returns The serialized error. | ||
| */ | ||
|
|
||
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| import { errorCodes } from "../src/jrpc/errors"; | ||
| import { JRPCEngine } from "../src/jrpc/jrpcEngine"; | ||
|
|
||
| describe("JRPCEngine request validation", () => { | ||
| it("returns invalidRequest for non-object requests", async () => { | ||
| const engine = new JRPCEngine(); | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| const response = await engine.handle(123 as any); | ||
| expect(response.error?.code).toBe(errorCodes.rpc.invalidRequest); | ||
| expect(response.id).toBeUndefined(); | ||
| expect(response.jsonrpc).toBe("2.0"); | ||
| }); | ||
|
|
||
| it("returns invalidRequest for non-string method", async () => { | ||
| const engine = new JRPCEngine(); | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| const response = await engine.handle({ id: 1, jsonrpc: "2.0", method: 123 as any }); | ||
| expect(response.error?.code).toBe(errorCodes.rpc.invalidRequest); | ||
| expect(response.id).toBe(1); | ||
| expect(response.jsonrpc).toBe("2.0"); | ||
| }); | ||
|
|
||
| it("returns invalidRequest for empty batch requests", async () => { | ||
| const engine = new JRPCEngine(); | ||
| const responses = await engine.handle([]); | ||
| expect(responses).toHaveLength(1); | ||
| expect(responses[0].error?.code).toBe(errorCodes.rpc.invalidRequest); | ||
| expect(responses[0].id).toBeUndefined(); | ||
| expect(responses[0].jsonrpc).toBe("2.0"); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
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.
Empty string produces NaN error code instead of fallback
Medium Severity
The
isValidNumberfunction usesNumber()for validation, which treats empty strings and whitespace-only strings as0. However, the calling code inconstructFallbackErrorusesparseInt()for the actual conversion, which returnsNaNfor these same inputs. Whencodeis an empty string,isValidNumber("")returnstrue(sinceNumber("")is0), butparseInt("")returnsNaN, resulting inNaNbeing used as the error code instead of the fallback value.Additional Locations (1)
src/jrpc/jrpcEngine.ts#L31-L32