Skip to content

Commit

Permalink
devDeps: @metamask/eslint-config* from v11 to v12 (#110)
Browse files Browse the repository at this point in the history
* devDeps: @metamask/eslint-config* from v11 to v12

* devDeps: bump typedoc to support typescript 4.9

* lint: s/T/Data/

* lint:fix

* jest-it-up

* yarn dedupe

* devDeps: dedupe @types/node
  • Loading branch information
legobeat authored Oct 17, 2023
1 parent 9200551 commit 9632f91
Show file tree
Hide file tree
Showing 6 changed files with 1,044 additions and 495 deletions.
4 changes: 2 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ module.exports = {
global: {
branches: 94.31,
functions: 94.11,
lines: 97.13,
statements: 97.13,
lines: 97.15,
statements: 97.15,
},
},

Expand Down
30 changes: 16 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,29 @@
"@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",
"@metamask/eslint-config": "^12.0.0",
"@metamask/eslint-config-jest": "^12.0.0",
"@metamask/eslint-config-nodejs": "^12.0.0",
"@metamask/eslint-config-typescript": "^12.0.0",
"@types/jest": "^28.1.6",
"@typescript-eslint/eslint-plugin": "^5.42.1",
"@typescript-eslint/parser": "^5.42.1",
"@types/node": "^16",
"@typescript-eslint/eslint-plugin": "^5.43.0",
"@typescript-eslint/parser": "^5.43.0",
"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",
"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",
"typedoc": "^0.23.28",
"typescript": "~4.9.5"
},
"packageManager": "yarn@3.2.4",
Expand Down
16 changes: 8 additions & 8 deletions src/classes.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
isPlainObject,
import type {
Json,
JsonRpcError as SerializedJsonRpcError,
} from '@metamask/utils';
import { isPlainObject } from '@metamask/utils';
import safeStringify from 'fast-safe-stringify';

import type { OptionalDataWithOptionalCause } from './utils';
Expand All @@ -17,13 +17,13 @@ export type { SerializedJsonRpcError };
* Permits any integer error code.
*/
export class JsonRpcError<
T extends OptionalDataWithOptionalCause,
Data extends OptionalDataWithOptionalCause,
> extends Error {
public code: number;

public data?: T;
public data?: Data;

constructor(code: number, message: string, data?: T) {
constructor(code: number, message: string, data?: Data) {
if (!Number.isInteger(code)) {
throw new Error('"code" must be an integer.');
}
Expand Down Expand Up @@ -84,8 +84,8 @@ export class JsonRpcError<
* Permits integer error codes in the [ 1000 <= 4999 ] range.
*/
export class EthereumProviderError<
T extends OptionalDataWithOptionalCause,
> extends JsonRpcError<T> {
Data extends OptionalDataWithOptionalCause,
> extends JsonRpcError<Data> {
/**
* Create an Ethereum Provider JSON-RPC error.
*
Expand All @@ -94,7 +94,7 @@ export class EthereumProviderError<
* @param message - The JSON-RPC error message.
* @param data - Optional data to include in the error.
*/
constructor(code: number, message: string, data?: T) {
constructor(code: number, message: string, data?: Data) {
if (!isValidEthProviderCode(code)) {
throw new Error(
'"code" must be an integer such that: 1000 <= code <= 4999',
Expand Down
110 changes: 56 additions & 54 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { JsonRpcError, EthereumProviderError } from './classes';
import { errorCodes } from './error-constants';
import { OptionalDataWithOptionalCause, getMessageFromCode } from './utils';
import type { OptionalDataWithOptionalCause } from './utils';
import { getMessageFromCode } from './utils';

type EthereumErrorOptions<T extends OptionalDataWithOptionalCause> = {
type EthereumErrorOptions<Data extends OptionalDataWithOptionalCause> = {
message?: string;
data?: T;
data?: Data;
};

type ServerErrorOptions<T extends OptionalDataWithOptionalCause> = {
type ServerErrorOptions<Data extends OptionalDataWithOptionalCause> = {
code: number;
} & EthereumErrorOptions<T>;
} & EthereumErrorOptions<Data>;

type CustomErrorArg<T extends OptionalDataWithOptionalCause> =
ServerErrorOptions<T>;
type CustomErrorArg<Data extends OptionalDataWithOptionalCause> =
ServerErrorOptions<Data>;

type JsonRpcErrorsArg<T extends OptionalDataWithOptionalCause> =
| EthereumErrorOptions<T>
type JsonRpcErrorsArg<Data extends OptionalDataWithOptionalCause> =
| EthereumErrorOptions<Data>
| string;

export const rpcErrors = {
Expand All @@ -25,17 +26,18 @@ export const rpcErrors = {
* @param arg - The error message or options bag.
* @returns An instance of the {@link JsonRpcError} class.
*/
parse: <T extends OptionalDataWithOptionalCause>(arg?: JsonRpcErrorsArg<T>) =>
getJsonRpcError(errorCodes.rpc.parse, arg),
parse: <Data extends OptionalDataWithOptionalCause>(
arg?: JsonRpcErrorsArg<Data>,
) => getJsonRpcError(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: <T extends OptionalDataWithOptionalCause>(
arg?: JsonRpcErrorsArg<T>,
invalidRequest: <Data extends OptionalDataWithOptionalCause>(
arg?: JsonRpcErrorsArg<Data>,
) => getJsonRpcError(errorCodes.rpc.invalidRequest, arg),

/**
Expand All @@ -44,8 +46,8 @@ export const rpcErrors = {
* @param arg - The error message or options bag.
* @returns An instance of the {@link JsonRpcError} class.
*/
invalidParams: <T extends OptionalDataWithOptionalCause>(
arg?: JsonRpcErrorsArg<T>,
invalidParams: <Data extends OptionalDataWithOptionalCause>(
arg?: JsonRpcErrorsArg<Data>,
) => getJsonRpcError(errorCodes.rpc.invalidParams, arg),

/**
Expand All @@ -54,8 +56,8 @@ export const rpcErrors = {
* @param arg - The error message or options bag.
* @returns An instance of the {@link JsonRpcError} class.
*/
methodNotFound: <T extends OptionalDataWithOptionalCause>(
arg?: JsonRpcErrorsArg<T>,
methodNotFound: <Data extends OptionalDataWithOptionalCause>(
arg?: JsonRpcErrorsArg<Data>,
) => getJsonRpcError(errorCodes.rpc.methodNotFound, arg),

/**
Expand All @@ -64,8 +66,8 @@ export const rpcErrors = {
* @param arg - The error message or options bag.
* @returns An instance of the {@link JsonRpcError} class.
*/
internal: <T extends OptionalDataWithOptionalCause>(
arg?: JsonRpcErrorsArg<T>,
internal: <Data extends OptionalDataWithOptionalCause>(
arg?: JsonRpcErrorsArg<Data>,
) => getJsonRpcError(errorCodes.rpc.internal, arg),

/**
Expand All @@ -76,8 +78,8 @@ export const rpcErrors = {
* @param opts - The error options bag.
* @returns An instance of the {@link JsonRpcError} class.
*/
server: <T extends OptionalDataWithOptionalCause>(
opts: ServerErrorOptions<T>,
server: <Data extends OptionalDataWithOptionalCause>(
opts: ServerErrorOptions<Data>,
) => {
if (!opts || typeof opts !== 'object' || Array.isArray(opts)) {
throw new Error(
Expand All @@ -99,8 +101,8 @@ export const rpcErrors = {
* @param arg - The error message or options bag.
* @returns An instance of the {@link JsonRpcError} class.
*/
invalidInput: <T extends OptionalDataWithOptionalCause>(
arg?: JsonRpcErrorsArg<T>,
invalidInput: <Data extends OptionalDataWithOptionalCause>(
arg?: JsonRpcErrorsArg<Data>,
) => getJsonRpcError(errorCodes.rpc.invalidInput, arg),

/**
Expand All @@ -109,8 +111,8 @@ export const rpcErrors = {
* @param arg - The error message or options bag.
* @returns An instance of the {@link JsonRpcError} class.
*/
resourceNotFound: <T extends OptionalDataWithOptionalCause>(
arg?: JsonRpcErrorsArg<T>,
resourceNotFound: <Data extends OptionalDataWithOptionalCause>(
arg?: JsonRpcErrorsArg<Data>,
) => getJsonRpcError(errorCodes.rpc.resourceNotFound, arg),

/**
Expand All @@ -119,8 +121,8 @@ export const rpcErrors = {
* @param arg - The error message or options bag.
* @returns An instance of the {@link JsonRpcError} class.
*/
resourceUnavailable: <T extends OptionalDataWithOptionalCause>(
arg?: JsonRpcErrorsArg<T>,
resourceUnavailable: <Data extends OptionalDataWithOptionalCause>(
arg?: JsonRpcErrorsArg<Data>,
) => getJsonRpcError(errorCodes.rpc.resourceUnavailable, arg),

/**
Expand All @@ -129,8 +131,8 @@ export const rpcErrors = {
* @param arg - The error message or options bag.
* @returns An instance of the {@link JsonRpcError} class.
*/
transactionRejected: <T extends OptionalDataWithOptionalCause>(
arg?: JsonRpcErrorsArg<T>,
transactionRejected: <Data extends OptionalDataWithOptionalCause>(
arg?: JsonRpcErrorsArg<Data>,
) => getJsonRpcError(errorCodes.rpc.transactionRejected, arg),

/**
Expand All @@ -139,8 +141,8 @@ export const rpcErrors = {
* @param arg - The error message or options bag.
* @returns An instance of the {@link JsonRpcError} class.
*/
methodNotSupported: <T extends OptionalDataWithOptionalCause>(
arg?: JsonRpcErrorsArg<T>,
methodNotSupported: <Data extends OptionalDataWithOptionalCause>(
arg?: JsonRpcErrorsArg<Data>,
) => getJsonRpcError(errorCodes.rpc.methodNotSupported, arg),

/**
Expand All @@ -149,8 +151,8 @@ export const rpcErrors = {
* @param arg - The error message or options bag.
* @returns An instance of the {@link JsonRpcError} class.
*/
limitExceeded: <T extends OptionalDataWithOptionalCause>(
arg?: JsonRpcErrorsArg<T>,
limitExceeded: <Data extends OptionalDataWithOptionalCause>(
arg?: JsonRpcErrorsArg<Data>,
) => getJsonRpcError(errorCodes.rpc.limitExceeded, arg),
};

Expand All @@ -161,8 +163,8 @@ export const providerErrors = {
* @param arg - The error message or options bag.
* @returns An instance of the {@link EthereumProviderError} class.
*/
userRejectedRequest: <T extends OptionalDataWithOptionalCause>(
arg?: JsonRpcErrorsArg<T>,
userRejectedRequest: <Data extends OptionalDataWithOptionalCause>(
arg?: JsonRpcErrorsArg<Data>,
) => {
return getEthProviderError(errorCodes.provider.userRejectedRequest, arg);
},
Expand All @@ -173,8 +175,8 @@ export const providerErrors = {
* @param arg - The error message or options bag.
* @returns An instance of the {@link EthereumProviderError} class.
*/
unauthorized: <T extends OptionalDataWithOptionalCause>(
arg?: JsonRpcErrorsArg<T>,
unauthorized: <Data extends OptionalDataWithOptionalCause>(
arg?: JsonRpcErrorsArg<Data>,
) => {
return getEthProviderError(errorCodes.provider.unauthorized, arg);
},
Expand All @@ -185,8 +187,8 @@ export const providerErrors = {
* @param arg - The error message or options bag.
* @returns An instance of the {@link EthereumProviderError} class.
*/
unsupportedMethod: <T extends OptionalDataWithOptionalCause>(
arg?: JsonRpcErrorsArg<T>,
unsupportedMethod: <Data extends OptionalDataWithOptionalCause>(
arg?: JsonRpcErrorsArg<Data>,
) => {
return getEthProviderError(errorCodes.provider.unsupportedMethod, arg);
},
Expand All @@ -197,8 +199,8 @@ export const providerErrors = {
* @param arg - The error message or options bag.
* @returns An instance of the {@link EthereumProviderError} class.
*/
disconnected: <T extends OptionalDataWithOptionalCause>(
arg?: JsonRpcErrorsArg<T>,
disconnected: <Data extends OptionalDataWithOptionalCause>(
arg?: JsonRpcErrorsArg<Data>,
) => {
return getEthProviderError(errorCodes.provider.disconnected, arg);
},
Expand All @@ -209,8 +211,8 @@ export const providerErrors = {
* @param arg - The error message or options bag.
* @returns An instance of the {@link EthereumProviderError} class.
*/
chainDisconnected: <T extends OptionalDataWithOptionalCause>(
arg?: JsonRpcErrorsArg<T>,
chainDisconnected: <Data extends OptionalDataWithOptionalCause>(
arg?: JsonRpcErrorsArg<Data>,
) => {
return getEthProviderError(errorCodes.provider.chainDisconnected, arg);
},
Expand All @@ -221,8 +223,8 @@ export const providerErrors = {
* @param opts - The error options bag.
* @returns An instance of the {@link EthereumProviderError} class.
*/
custom: <T extends OptionalDataWithOptionalCause>(
opts: CustomErrorArg<T>,
custom: <Data extends OptionalDataWithOptionalCause>(
opts: CustomErrorArg<Data>,
) => {
if (!opts || typeof opts !== 'object' || Array.isArray(opts)) {
throw new Error(
Expand All @@ -246,10 +248,10 @@ export const providerErrors = {
* @param arg - The error message or options bag.
* @returns An instance of the {@link JsonRpcError} class.
*/
function getJsonRpcError<T extends OptionalDataWithOptionalCause>(
function getJsonRpcError<Data extends OptionalDataWithOptionalCause>(
code: number,
arg?: JsonRpcErrorsArg<T>,
): JsonRpcError<T> {
arg?: JsonRpcErrorsArg<Data>,
): JsonRpcError<Data> {
const [message, data] = parseOpts(arg);
return new JsonRpcError(code, message ?? getMessageFromCode(code), data);
}
Expand All @@ -261,10 +263,10 @@ function getJsonRpcError<T extends OptionalDataWithOptionalCause>(
* @param arg - The error message or options bag.
* @returns An instance of the {@link EthereumProviderError} class.
*/
function getEthProviderError<T extends OptionalDataWithOptionalCause>(
function getEthProviderError<Data extends OptionalDataWithOptionalCause>(
code: number,
arg?: JsonRpcErrorsArg<T>,
): EthereumProviderError<T> {
arg?: JsonRpcErrorsArg<Data>,
): EthereumProviderError<Data> {
const [message, data] = parseOpts(arg);
return new EthereumProviderError(
code,
Expand All @@ -279,9 +281,9 @@ function getEthProviderError<T extends OptionalDataWithOptionalCause>(
* @param arg - The error message or options bag.
* @returns A tuple containing the error message and optional data.
*/
function parseOpts<T extends OptionalDataWithOptionalCause>(
arg?: JsonRpcErrorsArg<T>,
): [message?: string | undefined, data?: T | undefined] {
function parseOpts<Data extends OptionalDataWithOptionalCause>(
arg?: JsonRpcErrorsArg<Data>,
): [message?: string | undefined, data?: Data | undefined] {
if (arg) {
if (typeof arg === 'string') {
return [arg];
Expand Down
8 changes: 5 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type {
Json,
JsonRpcError as SerializedJsonRpcError,
RuntimeObject,
} from '@metamask/utils';
import {
hasProperty,
isValidJson,
isObject,
isJsonRpcError,
Json,
JsonRpcError as SerializedJsonRpcError,
RuntimeObject,
} from '@metamask/utils';

import { errorCodes, errorValues } from './error-constants';
Expand Down
Loading

0 comments on commit 9632f91

Please sign in to comment.