diff --git a/src/errors.ts b/src/errors.ts index ab13308..cb2c107 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -11,7 +11,7 @@ interface ServerErrorOptions extends EthereumErrorOptions { code: number; } -type CustomErrorOptions = ServerErrorOptions; +type CustomErrorArg = ServerErrorOptions; type EthErrorsArg = EthereumErrorOptions | string; @@ -21,35 +21,35 @@ export const ethErrors = { /** * Get a JSON RPC 2.0 Parse (-32700) error. */ - parse: (arg: EthErrorsArg) => getEthJsonRpcError( + parse: (arg?: EthErrorsArg) => getEthJsonRpcError( errorCodes.rpc.parse, arg, ), /** * Get a JSON RPC 2.0 Invalid Request (-32600) error. */ - invalidRequest: (arg: EthErrorsArg) => getEthJsonRpcError( + invalidRequest: (arg?: EthErrorsArg) => getEthJsonRpcError( errorCodes.rpc.invalidRequest, arg, ), /** * Get a JSON RPC 2.0 Invalid Params (-32602) error. */ - invalidParams: (arg: EthErrorsArg) => getEthJsonRpcError( + invalidParams: (arg?: EthErrorsArg) => getEthJsonRpcError( errorCodes.rpc.invalidParams, arg, ), /** * Get a JSON RPC 2.0 Method Not Found (-32601) error. */ - methodNotFound: (arg: EthErrorsArg) => getEthJsonRpcError( + methodNotFound: (arg?: EthErrorsArg) => getEthJsonRpcError( errorCodes.rpc.methodNotFound, arg, ), /** * Get a JSON RPC 2.0 Internal (-32603) error. */ - internal: (arg: EthErrorsArg) => getEthJsonRpcError( + internal: (arg?: EthErrorsArg) => getEthJsonRpcError( errorCodes.rpc.internal, arg, ), @@ -74,42 +74,42 @@ export const ethErrors = { /** * Get an Ethereum JSON RPC Invalid Input (-32000) error. */ - invalidInput: (arg: EthErrorsArg) => getEthJsonRpcError( + invalidInput: (arg?: EthErrorsArg) => getEthJsonRpcError( errorCodes.rpc.invalidInput, arg, ), /** * Get an Ethereum JSON RPC Resource Not Found (-32001) error. */ - resourceNotFound: (arg: EthErrorsArg) => getEthJsonRpcError( + resourceNotFound: (arg?: EthErrorsArg) => getEthJsonRpcError( errorCodes.rpc.resourceNotFound, arg, ), /** * Get an Ethereum JSON RPC Resource Unavailable (-32002) error. */ - resourceUnavailable: (arg: EthErrorsArg) => getEthJsonRpcError( + resourceUnavailable: (arg?: EthErrorsArg) => getEthJsonRpcError( errorCodes.rpc.resourceUnavailable, arg, ), /** * Get an Ethereum JSON RPC Transaction Rejected (-32003) error. */ - transactionRejected: (arg: EthErrorsArg) => getEthJsonRpcError( + transactionRejected: (arg?: EthErrorsArg) => getEthJsonRpcError( errorCodes.rpc.transactionRejected, arg, ), /** * Get an Ethereum JSON RPC Method Not Supported (-32004) error. */ - methodNotSupported: (arg: EthErrorsArg) => getEthJsonRpcError( + methodNotSupported: (arg?: EthErrorsArg) => getEthJsonRpcError( errorCodes.rpc.methodNotSupported, arg, ), /** * Get an Ethereum JSON RPC Limit Exceeded (-32005) error. */ - limitExceeded: (arg: EthErrorsArg) => getEthJsonRpcError( + limitExceeded: (arg?: EthErrorsArg) => getEthJsonRpcError( errorCodes.rpc.limitExceeded, arg, ), }, @@ -119,7 +119,7 @@ export const ethErrors = { /** * Get an Ethereum Provider User Rejected Request (4001) error. */ - userRejectedRequest: (arg: EthErrorsArg) => { + userRejectedRequest: (arg?: EthErrorsArg) => { return getEthProviderError( errorCodes.provider.userRejectedRequest, arg, ); @@ -128,7 +128,7 @@ export const ethErrors = { /** * Get an Ethereum Provider Unauthorized (4100) error. */ - unauthorized: (arg: EthErrorsArg) => { + unauthorized: (arg?: EthErrorsArg) => { return getEthProviderError( errorCodes.provider.unauthorized, arg, ); @@ -137,7 +137,7 @@ export const ethErrors = { /** * Get an Ethereum Provider Unsupported Method (4200) error. */ - unsupportedMethod: (arg: EthErrorsArg) => { + unsupportedMethod: (arg?: EthErrorsArg) => { return getEthProviderError( errorCodes.provider.unsupportedMethod, arg, ); @@ -146,7 +146,7 @@ export const ethErrors = { /** * Get an Ethereum Provider Not Connected (4900) error. */ - disconnected: (arg: EthErrorsArg) => { + disconnected: (arg?: EthErrorsArg) => { return getEthProviderError( errorCodes.provider.disconnected, arg, ); @@ -155,7 +155,7 @@ export const ethErrors = { /** * Get an Ethereum Provider Chain Not Connected (4901) error. */ - chainDisconnected: (arg: EthErrorsArg) => { + chainDisconnected: (arg?: EthErrorsArg) => { return getEthProviderError( errorCodes.provider.chainDisconnected, arg, ); @@ -164,11 +164,13 @@ export const ethErrors = { /** * Get a custom Ethereum Provider error. */ - custom: (opts: CustomErrorOptions) => { + custom: (opts: CustomErrorArg) => { 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', @@ -181,7 +183,7 @@ export const ethErrors = { // Internal -function getEthJsonRpcError(code: number, arg: EthErrorsArg): EthereumRpcError { +function getEthJsonRpcError(code: number, arg?: EthErrorsArg): EthereumRpcError { const [message, data] = parseOpts(arg); return new EthereumRpcError( code, @@ -190,7 +192,7 @@ function getEthJsonRpcError(code: number, arg: EthErrorsArg): EthereumRpcE ); } -function getEthProviderError(code: number, arg: EthErrorsArg): EthereumProviderError { +function getEthProviderError(code: number, arg?: EthErrorsArg): EthereumProviderError { const [message, data] = parseOpts(arg); return new EthereumProviderError( code, @@ -199,7 +201,7 @@ function getEthProviderError(code: number, arg: EthErrorsArg): EthereumPro ); } -function parseOpts(arg: EthErrorsArg): [string?, T?] { +function parseOpts(arg?: EthErrorsArg): [string?, T?] { if (arg) { if (typeof arg === 'string') { return [arg]; diff --git a/test/errors.js b/test/errors.js index ea8327e..31e8e6a 100644 --- a/test/errors.js +++ b/test/errors.js @@ -114,6 +114,11 @@ test('test exported object for correctness', (t) => { ); } }); + t.comment('Handles no argument.'); + validateError( + ethErrors.rpc.internal(), + 'internal', undefined, t, + ); t.comment('End: Ethereum RPC'); t.comment('Begin: Ethereum Provider'); @@ -137,6 +142,11 @@ test('test exported object for correctness', (t) => { ); } }); + t.comment('Handles no argument.'); + validateError( + ethErrors.provider.unauthorized(), + 'unauthorized', undefined, t, true, + ); t.comment('End: Ethereum Provider'); t.end(); });