Socket
Socket
Sign inDemoInstall

@metamask/rpc-errors

Package Overview
Dependencies
Maintainers
12
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@metamask/rpc-errors - npm Package Compare versions

Comparing version 6.1.0 to 6.2.0

dist/chunk-77LIU62I.js

56

CHANGELOG.md
# Changelog
All notable changes to this project will be documented in this file.

@@ -9,14 +10,30 @@

## [6.2.0]
### Added
- Add ESM build ([#133](https://github.com/MetaMask/rpc-errors/pull/133))
### Changed
- Update `@metamask/utils` from `^8.1.0` to `^8.3.0` ([#133](https://github.com/MetaMask/rpc-errors/pull/133))
## [6.1.0]
### Changed
- Update dependency `@metamask/utils` from `^8.0.0` to `^8.1.0` ([#108](https://github.com/MetaMask/rpc-errors/pull/108))
### Fixed
- Exclude `dist/__fixtures__` files from published package ([#114](https://github.com/MetaMask/rpc-errors/pull/114))
## [6.0.0]
### Changed
- Make Data type-parameter optional in JsonRpcError ([#102](https://github.com/MetaMask/rpc-errors/pull/102))
### Fixed
- **BREAKING**: `undefined` is now not recognized as valid JSON value

@@ -26,3 +43,5 @@ - Update dependency `@metamask/utils` from `^5.0.0` to `^8.0.0` ([#101](https://github.com/MetaMask/rpc-errors/pull/101))

## [5.1.1]
### Fixed
- Allow passing unknown values as cause ([#91](https://github.com/MetaMask/rpc-errors/pull/91))

@@ -32,3 +51,5 @@ - Prevously, only `Error` instances were allowed, but any value can be thrown as error

## [5.1.0]
### Added
- Allow passing a cause to predefined error functions ([#83](https://github.com/MetaMask/rpc-errors/pull/83))

@@ -39,3 +60,5 @@ - This allows passing an `Error` instance as cause, by using `{ data: { cause: /* some error */ } }`

## [5.0.0]
### Changed
- **BREAKING:** Bump minimum version to Node 16 ([#68](https://github.com/MetaMask/rpc-errors/pull/68))

@@ -52,18 +75,27 @@ - **BREAKING:** Rewrite error serialization ([#61](https://github.com/MetaMask/rpc-errors/pull/61))

## [4.0.3] - 2021-03-10
### Fixed
- Correctly type `ethErrors` getter function argument objects as optional ([#36](https://github.com/MetaMask/eth-rpc-errors/pull/36))
## [4.0.2] - 2020-11-17
### Changed
- Mark the `data` property of `EthereumRpcError` and `EthereumProviderError` as optional rather than `T | undefined` ([#34](https://github.com/MetaMask/eth-rpc-errors/pull/34))
### Fixed
- Correctly type `SerializedEthereumRpcError.stack` as `string`, if present ([#34](https://github.com/MetaMask/eth-rpc-errors/pull/34))
## [4.0.1] - 2020-11-03
### Changed
- Updated README.md ([#30](https://github.com/MetaMask/eth-rpc-errors/pull/30))
## [4.0.0] - 2020-11-02
### Changed
- **BREAKING:** `ERROR_CODES` export renamed to `errorCodes` ([#28](https://github.com/MetaMask/eth-rpc-errors/pull/28))

@@ -74,3 +106,5 @@ - **BREAKING:** `ethErrors` getters will now throw an error if passed a truthy, non-string `message` ([#28](https://github.com/MetaMask/eth-rpc-errors/pull/28))

## [3.0.0] - 2020-07-29
### Changed
- **BREAKING:** Second argument of `serializeError` is now an options object ([#22](https://github.com/MetaMask/eth-rpc-errors/pull/22))

@@ -80,7 +114,11 @@ - Error stacks are no longer serialized by default by `serializeError` ([#22](https://github.com/MetaMask/eth-rpc-errors/pull/22))

## [2.1.1] - 2020-05-12
### Changed
- Prevent unnecessary files from being published ([#17](https://github.com/MetaMask/eth-rpc-errors/pull/17))
## [2.1.0] - 2020-05-11
### Added
- New/missing errors:

@@ -94,14 +132,21 @@ - `ethErrors.provider` ([EIP-1193](https://eips.ethereum.org/EIPS/eip-1474#provider-errors))

### Changed
- Rename package to `eth-rpc-errors`
## [2.0.2] - 2020-02-12
### Changed
- Fix faulty null checks throughout codebase ([764832d](https://github.com/MetaMask/eth-rpc-errors/commit/764832d777f9274ca5bb9a6efa6958db2b640952))
## [2.0.1] - 2020-01-31
### Added
- Error codes in docstrings ([5452001](https://github.com/MetaMask/eth-rpc-errors/commit/545200100af05aeade62ba6b736f5080a6891bc4))
## [2.0.0] - 2019-09-26
### Changed
- **Exports**

@@ -116,3 +161,3 @@ - `errors` renamed `ethErrors`

- Added missing
[EIP-1474 errors](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1474.md)
[EIP-1474 errors](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1474.md)
- Added corresponding codes and messages

@@ -124,4 +169,4 @@ - **Namespacing**

- Formerly under `errors.eth`
- Most error getters now take a single, *optional* `opts` argument, which
is either a string or an object
- Most error getters now take a single, _optional_ `opts` argument, which
is either a string or an object
- If a string, it becomes the error message

@@ -138,7 +183,10 @@ - If an object, it should have the form: `{ message?: string, data?: any }`

## [1.1.0] - 2019-09-16
### Changed
- `serializeError`
- If the object passed to the function has a `.message` property, it will preferred over the `.message` property of the fallback error when creating the returned serialized error object
[Unreleased]: https://github.com/MetaMask/rpc-errors/compare/v6.1.0...HEAD
[Unreleased]: https://github.com/MetaMask/rpc-errors/compare/v6.2.0...HEAD
[6.2.0]: https://github.com/MetaMask/rpc-errors/compare/v6.1.0...v6.2.0
[6.1.0]: https://github.com/MetaMask/rpc-errors/compare/v6.0.0...v6.1.0

@@ -145,0 +193,0 @@ [6.0.0]: https://github.com/MetaMask/rpc-errors/compare/v5.1.1...v6.0.0

117

dist/classes.js

@@ -1,108 +0,11 @@

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EthereumProviderError = exports.JsonRpcError = void 0;
const utils_1 = require("@metamask/utils");
const fast_safe_stringify_1 = __importDefault(require("fast-safe-stringify"));
const utils_2 = require("./utils");
/**
* Error subclass implementing JSON RPC 2.0 errors and Ethereum RPC errors
* per EIP-1474.
*
* Permits any integer error code.
*/
class JsonRpcError extends Error {
constructor(code, message, data) {
if (!Number.isInteger(code)) {
throw new Error('"code" must be an integer.');
}
if (!message || typeof message !== 'string') {
throw new Error('"message" must be a non-empty string.');
}
super(message);
this.code = code;
if (data !== undefined) {
this.data = data;
}
}
/**
* Get the error as JSON-serializable object.
*
* @returns A plain object with all public class properties.
*/
serialize() {
const serialized = {
code: this.code,
message: this.message,
};
if (this.data !== undefined) {
// `this.data` is not guaranteed to be a plain object, but this simplifies
// the type guard below. We can safely cast it because we know it's a
// JSON-serializable value.
serialized.data = this.data;
if ((0, utils_1.isPlainObject)(this.data)) {
serialized.data.cause = (0, utils_2.serializeCause)(this.data.cause);
}
}
if (this.stack) {
serialized.stack = this.stack;
}
return serialized;
}
/**
* Get a string representation of the serialized error, omitting any circular
* references.
*
* @returns A string representation of the serialized error.
*/
toString() {
return (0, fast_safe_stringify_1.default)(this.serialize(), stringifyReplacer, 2);
}
}
exports.JsonRpcError = JsonRpcError;
/**
* Error subclass implementing Ethereum Provider errors per EIP-1193.
* Permits integer error codes in the [ 1000 <= 4999 ] range.
*/
class EthereumProviderError extends JsonRpcError {
/**
* Create an Ethereum Provider JSON-RPC error.
*
* @param code - The JSON-RPC error code. Must be an integer in the
* `1000 <= n <= 4999` range.
* @param message - The JSON-RPC error message.
* @param data - Optional data to include in the error.
*/
constructor(code, message, data) {
if (!isValidEthProviderCode(code)) {
throw new Error('"code" must be an integer such that: 1000 <= code <= 4999');
}
super(code, message, data);
}
}
exports.EthereumProviderError = EthereumProviderError;
/**
* Check if the given code is a valid JSON-RPC error code.
*
* @param code - The code to check.
* @returns Whether the code is valid.
*/
function isValidEthProviderCode(code) {
return Number.isInteger(code) && code >= 1000 && code <= 4999;
}
/**
* A JSON replacer function that omits circular references.
*
* @param _ - The key being replaced.
* @param value - The value being replaced.
* @returns The value to use in place of the original value.
*/
function stringifyReplacer(_, value) {
if (value === '[Circular]') {
return undefined;
}
return value;
}
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
var _chunk77LIU62Ijs = require('./chunk-77LIU62I.js');
require('./chunk-LIUXO4DW.js');
require('./chunk-FBHPY3A4.js');
exports.EthereumProviderError = _chunk77LIU62Ijs.EthereumProviderError; exports.JsonRpcError = _chunk77LIU62Ijs.JsonRpcError;
//# sourceMappingURL=classes.js.map

@@ -1,94 +0,9 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.errorValues = exports.errorCodes = void 0;
exports.errorCodes = {
rpc: {
invalidInput: -32000,
resourceNotFound: -32001,
resourceUnavailable: -32002,
transactionRejected: -32003,
methodNotSupported: -32004,
limitExceeded: -32005,
parse: -32700,
invalidRequest: -32600,
methodNotFound: -32601,
invalidParams: -32602,
internal: -32603,
},
provider: {
userRejectedRequest: 4001,
unauthorized: 4100,
unsupportedMethod: 4200,
disconnected: 4900,
chainDisconnected: 4901,
},
};
/* eslint-disable @typescript-eslint/naming-convention */
exports.errorValues = {
'-32700': {
standard: 'JSON RPC 2.0',
message: 'Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text.',
},
'-32600': {
standard: 'JSON RPC 2.0',
message: 'The JSON sent is not a valid Request object.',
},
'-32601': {
standard: 'JSON RPC 2.0',
message: 'The method does not exist / is not available.',
},
'-32602': {
standard: 'JSON RPC 2.0',
message: 'Invalid method parameter(s).',
},
'-32603': {
standard: 'JSON RPC 2.0',
message: 'Internal JSON-RPC error.',
},
'-32000': {
standard: 'EIP-1474',
message: 'Invalid input.',
},
'-32001': {
standard: 'EIP-1474',
message: 'Resource not found.',
},
'-32002': {
standard: 'EIP-1474',
message: 'Resource unavailable.',
},
'-32003': {
standard: 'EIP-1474',
message: 'Transaction rejected.',
},
'-32004': {
standard: 'EIP-1474',
message: 'Method not supported.',
},
'-32005': {
standard: 'EIP-1474',
message: 'Request limit exceeded.',
},
'4001': {
standard: 'EIP-1193',
message: 'User rejected the request.',
},
'4100': {
standard: 'EIP-1193',
message: 'The requested account and/or method has not been authorized by the user.',
},
'4200': {
standard: 'EIP-1193',
message: 'The requested method is not supported by this Ethereum provider.',
},
'4900': {
standard: 'EIP-1193',
message: 'The provider is disconnected from all chains.',
},
'4901': {
standard: 'EIP-1193',
message: 'The provider is disconnected from the specified chain.',
},
};
/* eslint-enable @typescript-eslint/naming-convention */
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
var _chunkFBHPY3A4js = require('./chunk-FBHPY3A4.js');
exports.errorCodes = _chunkFBHPY3A4js.errorCodes; exports.errorValues = _chunkFBHPY3A4js.errorValues;
//# sourceMappingURL=error-constants.js.map

@@ -1,210 +0,12 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.providerErrors = exports.rpcErrors = void 0;
const classes_1 = require("./classes");
const error_constants_1 = require("./error-constants");
const utils_1 = require("./utils");
exports.rpcErrors = {
/**
* Get a JSON RPC 2.0 Parse (-32700) error.
*
* @param arg - The error message or options bag.
* @returns An instance of the {@link JsonRpcError} class.
*/
parse: (arg) => getJsonRpcError(error_constants_1.errorCodes.rpc.parse, arg),
/**
* Get a JSON RPC 2.0 Invalid Request (-32600) error.
*
* @param arg - The error message or options bag.
* @returns An instance of the {@link JsonRpcError} class.
*/
invalidRequest: (arg) => getJsonRpcError(error_constants_1.errorCodes.rpc.invalidRequest, arg),
/**
* Get a JSON RPC 2.0 Invalid Params (-32602) error.
*
* @param arg - The error message or options bag.
* @returns An instance of the {@link JsonRpcError} class.
*/
invalidParams: (arg) => getJsonRpcError(error_constants_1.errorCodes.rpc.invalidParams, arg),
/**
* Get a JSON RPC 2.0 Method Not Found (-32601) error.
*
* @param arg - The error message or options bag.
* @returns An instance of the {@link JsonRpcError} class.
*/
methodNotFound: (arg) => getJsonRpcError(error_constants_1.errorCodes.rpc.methodNotFound, arg),
/**
* Get a JSON RPC 2.0 Internal (-32603) error.
*
* @param arg - The error message or options bag.
* @returns An instance of the {@link JsonRpcError} class.
*/
internal: (arg) => getJsonRpcError(error_constants_1.errorCodes.rpc.internal, arg),
/**
* Get a JSON RPC 2.0 Server error.
* Permits integer error codes in the [ -32099 <= -32005 ] range.
* Codes -32000 through -32004 are reserved by EIP-1474.
*
* @param opts - The error options bag.
* @returns An instance of the {@link JsonRpcError} class.
*/
server: (opts) => {
if (!opts || typeof opts !== 'object' || Array.isArray(opts)) {
throw new Error('Ethereum RPC Server errors must provide single object argument.');
}
const { code } = opts;
if (!Number.isInteger(code) || code > -32005 || code < -32099) {
throw new Error('"code" must be an integer such that: -32099 <= code <= -32005');
}
return getJsonRpcError(code, opts);
},
/**
* Get an Ethereum JSON RPC Invalid Input (-32000) error.
*
* @param arg - The error message or options bag.
* @returns An instance of the {@link JsonRpcError} class.
*/
invalidInput: (arg) => getJsonRpcError(error_constants_1.errorCodes.rpc.invalidInput, arg),
/**
* Get an Ethereum JSON RPC Resource Not Found (-32001) error.
*
* @param arg - The error message or options bag.
* @returns An instance of the {@link JsonRpcError} class.
*/
resourceNotFound: (arg) => getJsonRpcError(error_constants_1.errorCodes.rpc.resourceNotFound, arg),
/**
* Get an Ethereum JSON RPC Resource Unavailable (-32002) error.
*
* @param arg - The error message or options bag.
* @returns An instance of the {@link JsonRpcError} class.
*/
resourceUnavailable: (arg) => getJsonRpcError(error_constants_1.errorCodes.rpc.resourceUnavailable, arg),
/**
* Get an Ethereum JSON RPC Transaction Rejected (-32003) error.
*
* @param arg - The error message or options bag.
* @returns An instance of the {@link JsonRpcError} class.
*/
transactionRejected: (arg) => getJsonRpcError(error_constants_1.errorCodes.rpc.transactionRejected, arg),
/**
* Get an Ethereum JSON RPC Method Not Supported (-32004) error.
*
* @param arg - The error message or options bag.
* @returns An instance of the {@link JsonRpcError} class.
*/
methodNotSupported: (arg) => getJsonRpcError(error_constants_1.errorCodes.rpc.methodNotSupported, arg),
/**
* Get an Ethereum JSON RPC Limit Exceeded (-32005) error.
*
* @param arg - The error message or options bag.
* @returns An instance of the {@link JsonRpcError} class.
*/
limitExceeded: (arg) => getJsonRpcError(error_constants_1.errorCodes.rpc.limitExceeded, arg),
};
exports.providerErrors = {
/**
* Get an Ethereum Provider User Rejected Request (4001) error.
*
* @param arg - The error message or options bag.
* @returns An instance of the {@link EthereumProviderError} class.
*/
userRejectedRequest: (arg) => {
return getEthProviderError(error_constants_1.errorCodes.provider.userRejectedRequest, arg);
},
/**
* Get an Ethereum Provider Unauthorized (4100) error.
*
* @param arg - The error message or options bag.
* @returns An instance of the {@link EthereumProviderError} class.
*/
unauthorized: (arg) => {
return getEthProviderError(error_constants_1.errorCodes.provider.unauthorized, arg);
},
/**
* Get an Ethereum Provider Unsupported Method (4200) error.
*
* @param arg - The error message or options bag.
* @returns An instance of the {@link EthereumProviderError} class.
*/
unsupportedMethod: (arg) => {
return getEthProviderError(error_constants_1.errorCodes.provider.unsupportedMethod, arg);
},
/**
* Get an Ethereum Provider Not Connected (4900) error.
*
* @param arg - The error message or options bag.
* @returns An instance of the {@link EthereumProviderError} class.
*/
disconnected: (arg) => {
return getEthProviderError(error_constants_1.errorCodes.provider.disconnected, arg);
},
/**
* Get an Ethereum Provider Chain Not Connected (4901) error.
*
* @param arg - The error message or options bag.
* @returns An instance of the {@link EthereumProviderError} class.
*/
chainDisconnected: (arg) => {
return getEthProviderError(error_constants_1.errorCodes.provider.chainDisconnected, arg);
},
/**
* Get a custom Ethereum Provider error.
*
* @param opts - The error options bag.
* @returns An instance of the {@link EthereumProviderError} class.
*/
custom: (opts) => {
if (!opts || typeof opts !== 'object' || Array.isArray(opts)) {
throw new Error('Ethereum Provider custom errors must provide single object argument.');
}
const { code, message, data } = opts;
if (!message || typeof message !== 'string') {
throw new Error('"message" must be a nonempty string');
}
return new classes_1.EthereumProviderError(code, message, data);
},
};
/**
* Get a generic JSON-RPC error class instance.
*
* @param code - The error code.
* @param arg - The error message or options bag.
* @returns An instance of the {@link JsonRpcError} class.
*/
function getJsonRpcError(code, arg) {
const [message, data] = parseOpts(arg);
return new classes_1.JsonRpcError(code, message ?? (0, utils_1.getMessageFromCode)(code), data);
}
/**
* Get an Ethereum Provider error class instance.
*
* @param code - The error code.
* @param arg - The error message or options bag.
* @returns An instance of the {@link EthereumProviderError} class.
*/
function getEthProviderError(code, arg) {
const [message, data] = parseOpts(arg);
return new classes_1.EthereumProviderError(code, message ?? (0, utils_1.getMessageFromCode)(code), data);
}
/**
* Get an error message and optional data from an options bag.
*
* @param arg - The error message or options bag.
* @returns A tuple containing the error message and optional data.
*/
function parseOpts(arg) {
if (arg) {
if (typeof arg === 'string') {
return [arg];
}
else if (typeof arg === 'object' && !Array.isArray(arg)) {
const { message, data } = arg;
if (message && typeof message !== 'string') {
throw new Error('Must specify string message.');
}
return [message ?? undefined, data];
}
}
return [];
}
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
var _chunkI3KUC4QQjs = require('./chunk-I3KUC4QQ.js');
require('./chunk-77LIU62I.js');
require('./chunk-LIUXO4DW.js');
require('./chunk-FBHPY3A4.js');
exports.providerErrors = _chunkI3KUC4QQjs.providerErrors; exports.rpcErrors = _chunkI3KUC4QQjs.rpcErrors;
//# sourceMappingURL=errors.js.map

@@ -1,16 +0,27 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.errorCodes = exports.providerErrors = exports.rpcErrors = exports.getMessageFromCode = exports.serializeError = exports.serializeCause = exports.EthereumProviderError = exports.JsonRpcError = void 0;
var classes_1 = require("./classes");
Object.defineProperty(exports, "JsonRpcError", { enumerable: true, get: function () { return classes_1.JsonRpcError; } });
Object.defineProperty(exports, "EthereumProviderError", { enumerable: true, get: function () { return classes_1.EthereumProviderError; } });
var utils_1 = require("./utils");
Object.defineProperty(exports, "serializeCause", { enumerable: true, get: function () { return utils_1.serializeCause; } });
Object.defineProperty(exports, "serializeError", { enumerable: true, get: function () { return utils_1.serializeError; } });
Object.defineProperty(exports, "getMessageFromCode", { enumerable: true, get: function () { return utils_1.getMessageFromCode; } });
var errors_1 = require("./errors");
Object.defineProperty(exports, "rpcErrors", { enumerable: true, get: function () { return errors_1.rpcErrors; } });
Object.defineProperty(exports, "providerErrors", { enumerable: true, get: function () { return errors_1.providerErrors; } });
var error_constants_1 = require("./error-constants");
Object.defineProperty(exports, "errorCodes", { enumerable: true, get: function () { return error_constants_1.errorCodes; } });
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
var _chunkI3KUC4QQjs = require('./chunk-I3KUC4QQ.js');
var _chunk77LIU62Ijs = require('./chunk-77LIU62I.js');
var _chunkLIUXO4DWjs = require('./chunk-LIUXO4DW.js');
var _chunkFBHPY3A4js = require('./chunk-FBHPY3A4.js');
exports.EthereumProviderError = _chunk77LIU62Ijs.EthereumProviderError; exports.JsonRpcError = _chunk77LIU62Ijs.JsonRpcError; exports.errorCodes = _chunkFBHPY3A4js.errorCodes; exports.getMessageFromCode = _chunkLIUXO4DWjs.getMessageFromCode; exports.providerErrors = _chunkI3KUC4QQjs.providerErrors; exports.rpcErrors = _chunkI3KUC4QQjs.rpcErrors; exports.serializeCause = _chunkLIUXO4DWjs.serializeCause; exports.serializeError = _chunkLIUXO4DWjs.serializeError;
//# sourceMappingURL=index.js.map

@@ -1,148 +0,16 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.serializeCause = exports.serializeError = exports.isValidCode = exports.getMessageFromCode = exports.JSON_RPC_SERVER_ERROR_MESSAGE = void 0;
const utils_1 = require("@metamask/utils");
const error_constants_1 = require("./error-constants");
const FALLBACK_ERROR_CODE = error_constants_1.errorCodes.rpc.internal;
const FALLBACK_MESSAGE = 'Unspecified error message. This is a bug, please report it.';
const FALLBACK_ERROR = {
code: FALLBACK_ERROR_CODE,
message: getMessageFromCode(FALLBACK_ERROR_CODE),
};
exports.JSON_RPC_SERVER_ERROR_MESSAGE = 'Unspecified server error.';
/**
* Gets the message for a given code, or a fallback message if the code has
* no corresponding message.
*
* @param code - The error code.
* @param fallbackMessage - The fallback message to use if the code has no
* corresponding message.
* @returns The message for the given code, or the fallback message if the code
* has no corresponding message.
*/
function getMessageFromCode(code, fallbackMessage = FALLBACK_MESSAGE) {
if (isValidCode(code)) {
const codeString = code.toString();
if ((0, utils_1.hasProperty)(error_constants_1.errorValues, codeString)) {
return error_constants_1.errorValues[codeString].message;
}
if (isJsonRpcServerError(code)) {
return exports.JSON_RPC_SERVER_ERROR_MESSAGE;
}
}
return fallbackMessage;
}
exports.getMessageFromCode = getMessageFromCode;
/**
* Returns whether the given code is valid.
* A code is valid if it is an integer.
*
* @param code - The error code.
* @returns Whether the given code is valid.
*/
function isValidCode(code) {
return Number.isInteger(code);
}
exports.isValidCode = isValidCode;
/**
* Serializes the given error to an Ethereum JSON RPC-compatible error object.
* If the given error is not fully compatible, it will be preserved on the
* returned object's data.cause property.
*
* @param error - The error to serialize.
* @param options - Options bag.
* @param options.fallbackError - The error to return if the given error is
* not compatible. Should be a JSON serializable value.
* @param options.shouldIncludeStack - Whether to include the error's stack
* on the returned object.
* @returns The serialized error.
*/
function serializeError(error, { fallbackError = FALLBACK_ERROR, shouldIncludeStack = true } = {}) {
if (!(0, utils_1.isJsonRpcError)(fallbackError)) {
throw new Error('Must provide fallback error with integer number code and string message.');
}
const serialized = buildError(error, fallbackError);
if (!shouldIncludeStack) {
delete serialized.stack;
}
return serialized;
}
exports.serializeError = serializeError;
/**
* 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, fallbackError) {
// 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();
}
if ((0, utils_1.isJsonRpcError)(error)) {
return 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,
data: { cause },
};
return fallbackWithCause;
}
/**
* Check if the given code is a valid JSON-RPC server error code.
*
* @param code - The error code.
* @returns Whether the given code is a valid JSON-RPC server error code.
*/
function isJsonRpcServerError(code) {
return code >= -32099 && code <= -32000;
}
/**
* Serializes an unknown error to be used as the `cause` in a fallback error.
*
* @param error - The unknown error.
* @returns A JSON-serializable object containing as much information about the original error as possible.
*/
function serializeCause(error) {
if (Array.isArray(error)) {
return error.map((entry) => {
if ((0, utils_1.isValidJson)(entry)) {
return entry;
}
else if ((0, utils_1.isObject)(entry)) {
return serializeObject(entry);
}
return null;
});
}
else if ((0, utils_1.isObject)(error)) {
return serializeObject(error);
}
if ((0, utils_1.isValidJson)(error)) {
return error;
}
return null;
}
exports.serializeCause = serializeCause;
/**
* Extracts all JSON-serializable properties from an object.
*
* @param object - The object in question.
* @returns An object containing all the JSON-serializable properties.
*/
function serializeObject(object) {
return Object.getOwnPropertyNames(object).reduce((acc, key) => {
const value = object[key];
if ((0, utils_1.isValidJson)(value)) {
acc[key] = value;
}
return acc;
}, {});
}
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
var _chunkLIUXO4DWjs = require('./chunk-LIUXO4DW.js');
require('./chunk-FBHPY3A4.js');
exports.JSON_RPC_SERVER_ERROR_MESSAGE = _chunkLIUXO4DWjs.JSON_RPC_SERVER_ERROR_MESSAGE; exports.getMessageFromCode = _chunkLIUXO4DWjs.getMessageFromCode; exports.isValidCode = _chunkLIUXO4DWjs.isValidCode; exports.serializeCause = _chunkLIUXO4DWjs.serializeCause; exports.serializeError = _chunkLIUXO4DWjs.serializeError;
//# sourceMappingURL=utils.js.map
{
"name": "@metamask/rpc-errors",
"version": "6.1.0",
"description": "Ethereum RPC and Provider errors.",
"version": "6.2.0",
"description": "Ethereum RPC and Provider errors",
"keywords": [

@@ -21,18 +21,28 @@ "rpc",

"author": "Erik Marks <rekmarks@protonmail.com>",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js",
"types": "./dist/types/index.d.ts"
},
"./package.json": "./package.json"
},
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/types/index.d.ts",
"files": [
"dist",
"!dist/__fixtures__"
"dist"
],
"scripts": {
"build": "tsc --project tsconfig.build.json",
"build": "tsup --clean && yarn build:types",
"build:clean": "rimraf dist && yarn build",
"build:docs": "typedoc",
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn lint:dependencies --check && yarn lint:changelog",
"lint:changelog": "auto-changelog validate",
"build:types": "tsc --project tsconfig.build.json",
"lint": "yarn lint:eslint && yarn lint:constraints && yarn lint:misc --check && yarn lint:dependencies --check && yarn lint:changelog",
"lint:changelog": "auto-changelog validate --prettier",
"lint:constraints": "yarn constraints",
"lint:dependencies": "depcheck && yarn dedupe",
"lint:eslint": "eslint . --cache --ext js,ts",
"lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write && yarn lint:dependencies",
"lint:misc": "prettier '**/*.json' '**/*.md' '!CHANGELOG.md' '**/*.yml' '!.yarnrc.yml' --ignore-path .gitignore --no-error-on-unmatched-pattern",
"lint:fix": "yarn lint:eslint --fix && yarn lint:constraints --fix && yarn lint:misc --write && yarn lint:dependencies && yarn lint:changelog",
"lint:misc": "prettier '**/*.json' '**/*.md' '**/*.yml' '!.yarnrc.yml' --ignore-path .gitignore --no-error-on-unmatched-pattern",
"prepack": "./scripts/prepack.sh",

@@ -43,30 +53,34 @@ "test": "jest && jest-it-up",

"dependencies": {
"@metamask/utils": "^8.1.0",
"@metamask/utils": "^8.3.0",
"fast-safe-stringify": "^2.0.6"
},
"devDependencies": {
"@lavamoat/allow-scripts": "^2.3.1",
"@lavamoat/preinstall-always-fail": "^1.0.0",
"@metamask/auto-changelog": "^3.2.0",
"@metamask/eslint-config": "^11.1.0",
"@metamask/eslint-config-jest": "^11.1.0",
"@metamask/eslint-config-nodejs": "^11.1.0",
"@metamask/eslint-config-typescript": "^11.1.0",
"@lavamoat/allow-scripts": "^3.0.0",
"@lavamoat/preinstall-always-fail": "^2.0.0",
"@metamask/auto-changelog": "^3.4.3",
"@metamask/eslint-config": "^12.2.0",
"@metamask/eslint-config-jest": "^12.1.0",
"@metamask/eslint-config-nodejs": "^12.1.0",
"@metamask/eslint-config-typescript": "^12.1.0",
"@types/jest": "^28.1.6",
"@typescript-eslint/eslint-plugin": "^5.42.1",
"@typescript-eslint/parser": "^5.42.1",
"depcheck": "^1.4.5",
"eslint": "^8.27.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^27.1.5",
"eslint-plugin-jsdoc": "^39.6.2",
"eslint-plugin-node": "^11.1.0",
"@types/node": "^16",
"@typescript-eslint/eslint-plugin": "^5.43.0",
"@typescript-eslint/parser": "^5.43.0",
"depcheck": "^1.4.7",
"eslint": "^8.44.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-jest": "^27.2.2",
"eslint-plugin-jsdoc": "^39.9.1",
"eslint-plugin-n": "^15.7.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-promise": "^6.1.1",
"jest": "^28.1.3",
"jest-it-up": "^2.0.2",
"prettier": "^2.7.1",
"prettier-plugin-packagejson": "^2.2.18",
"prettier-plugin-packagejson": "^2.3.0",
"ts-jest": "^28.0.7",
"typedoc": "^0.23.19",
"ts-node": "^10.7.0",
"tsup": "^7.2.0",
"typedoc": "^0.23.28",
"typescript": "~4.9.5"

@@ -84,5 +98,6 @@ },

"allowScripts": {
"@lavamoat/preinstall-always-fail": false
"@lavamoat/preinstall-always-fail": false,
"tsup>esbuild": false
}
}
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc