Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 4 additions & 16 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { errorCodes, errorValues } from './error-constants';
import { EthereumRpcError, SerializedEthereumRpcError } from './classes';

const FALLBACK_ERROR_CODE = errorCodes.rpc.internal;
const FALLBACK_MESSAGE = 'Unspecified error message. This is a bug, please report it.';
const FALLBACK_MESSAGE = 'Invalid internal error. See "data.originalError" for original value. Please report this bug.';
const FALLBACK_ERROR: SerializedEthereumRpcError = {
code: FALLBACK_ERROR_CODE,
message: getMessageFromCode(FALLBACK_ERROR_CODE),
Expand All @@ -20,7 +20,7 @@ export function getMessageFromCode(
code: number,
fallbackMessage: string = FALLBACK_MESSAGE,
): string {
if (Number.isInteger(code)) {
if (isValidCode(code)) {
const codeString = code.toString();

if (hasKey(errorValues, codeString)) {
Expand All @@ -35,22 +35,10 @@ export function getMessageFromCode(

/**
* Returns whether the given code is valid.
* A code is only valid if it has a message.
* A code is valid if it is an integer number.
*/
export function isValidCode(code: number): boolean {
if (!Number.isInteger(code)) {
return false;
}

const codeString = code.toString();
if (errorValues[codeString as ErrorValueKey]) {
return true;
}

if (isJsonRpcServerError(code)) {
return true;
}
return false;
return Number.isInteger(code);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions test/serializeError.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ const dummyMessage = 'baz';

const invalidError0 = 0;
const invalidError1 = ['foo', 'bar', 3];
const invalidError2 = { code: 34 };
const invalidError2 = { code: 'Invalid code, should be an integer' };
const invalidError3 = { code: 4001 };
const invalidError4 = { code: 4001, message: 3, data: Object.assign({}, dummyData) };
const invalidError5 = null;
const invalidError6 = undefined;
const invalidError7 = { code: 34, message: dummyMessage, data: Object.assign({}, dummyData) };
const invalidError7 = { code: 'Invalid code, should be an integer', message: dummyMessage, data: Object.assign({}, dummyData) };

const validError0 = { code: 4001, message: dummyMessage };
const validError1 = { code: 4001, message: dummyMessage, data: Object.assign({}, dummyData) };
Expand Down