-
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
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e194090
chore: replace hardcoded error codes with rpc error constants
lwin-kyaw 77f69c9
chore: revert package-lock.json
lwin-kyaw ee219ea
chore: updated tests
lwin-kyaw 891890f
feat: added 'cleanup' to the stream after close
lwin-kyaw 0b601ce
chore: added more tests for JrpcEngine and Middlewares
lwin-kyaw be66bf7
feat: types/interfaces updates and refactor utils
lwin-kyaw d89bc3e
chore: updated tests and types
lwin-kyaw 8472206
fix: updated 'isJRPCRequest' function to check for undefined value
lwin-kyaw 3bef878
chore: updated 'JRPCParams' type
lwin-kyaw 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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import { hasProperty, isObject } from "../../utils"; | ||
| import { JRPCError, Json } from "../interfaces"; | ||
| import { errorCodes, errorValues } from "./error-constants"; | ||
|
|
||
|
|
@@ -9,6 +10,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; | ||
| } | ||
| } | ||
chaitanyapotti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Returns whether the given code is valid. | ||
| * A code is valid if it is an integer. | ||
|
|
@@ -24,17 +38,6 @@ | |
| return typeof value === "string" && value.length > 0; | ||
| } | ||
|
|
||
| /** | ||
| * A type guard for {@link RuntimeObject}. | ||
| * | ||
| * @param value - The value to check. | ||
| * @returns Whether the specified value has a runtime type of `object` and is | ||
| * neither `null` nor an `Array`. | ||
| */ | ||
| export function isObject(value: unknown): value is Record<PropertyKey, unknown> { | ||
| return Boolean(value) && typeof value === "object" && !Array.isArray(value); | ||
| } | ||
|
|
||
| /** | ||
| * Check if the value is plain object. | ||
| * | ||
|
|
@@ -176,14 +179,27 @@ | |
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Attempts to extract the original `message` property from an error value of uncertain shape. | ||
| * | ||
| * @param error - The error in question. | ||
| * @returns The original message, if it exists and is a non-empty string. | ||
| */ | ||
| function getOriginalMessage(error: unknown): string | undefined { | ||
| if (isObject(error) && hasProperty(error, "message") && typeof error.message === "string" && error.message.length > 0) { | ||
| return error.message; | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Construct a JSON-serializable object given an error and a JSON serializable `fallbackError` | ||
| * | ||
| * @param error - The error in question. | ||
| * @param fallbackError - A JSON serializable fallback error. | ||
| * @returns A JSON serializable error object. | ||
| */ | ||
| function buildError(error: unknown, fallbackError: JRPCError): JRPCError { | ||
| function buildError(error: unknown, fallbackError: JRPCError, shouldPreserveMessage: boolean): JRPCError { | ||
| // If an error specifies a `serialize` function, we call it and return the result. | ||
| if (error && typeof error === "object" && "serialize" in error && typeof error.serialize === "function") { | ||
| return error.serialize(); | ||
|
|
@@ -193,10 +209,13 @@ | |
| return error as JRPCError; | ||
| } | ||
|
|
||
| const originalMessage = getOriginalMessage(error); | ||
|
|
||
| // If the error does not match the JsonRpcError type, use the fallback error, but try to include the original error as `cause`. | ||
| const cause = serializeCause(error); | ||
| const fallbackWithCause = { | ||
| ...fallbackError, | ||
| ...(shouldPreserveMessage && originalMessage && { message: originalMessage }), | ||
| data: { cause }, | ||
| }; | ||
|
|
||
|
|
@@ -210,18 +229,21 @@ | |
| * | ||
| * @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 232 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 234 in src/jrpc/errors/utils.ts
|
||
| * on the returned object. | ||
| * @returns The serialized error. | ||
| */ | ||
| export function serializeJrpcError(error: unknown, { fallbackError = FALLBACK_ERROR, shouldIncludeStack = true } = {}): JRPCError { | ||
| export function serializeJrpcError( | ||
| error: unknown, | ||
| { fallbackError = FALLBACK_ERROR, shouldIncludeStack = true, shouldPreserveMessage = true } = {} | ||
| ): JRPCError { | ||
| if (!isJsonRpcError(fallbackError)) { | ||
| throw new Error("Must provide fallback error with integer number code and string message."); | ||
| } | ||
|
|
||
| const serialized = buildError(error, fallbackError); | ||
| const serialized = buildError(error, fallbackError, shouldPreserveMessage); | ||
|
|
||
| if (!shouldIncludeStack) { | ||
| delete serialized.stack; | ||
|
|
||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.