Skip to content

Commit

Permalink
Make error getter argument object optional (#36)
Browse files Browse the repository at this point in the history
* Make error getter argument object optional
* Add test cases for no argument
  • Loading branch information
rekmarks authored Jan 11, 2021
1 parent 2a699eb commit 22089c1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
44 changes: 23 additions & 21 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface ServerErrorOptions<T> extends EthereumErrorOptions<T> {
code: number;
}

type CustomErrorOptions<T> = ServerErrorOptions<T>;
type CustomErrorArg<T> = ServerErrorOptions<T>;

type EthErrorsArg<T> = EthereumErrorOptions<T> | string;

Expand All @@ -21,35 +21,35 @@ export const ethErrors = {
/**
* Get a JSON RPC 2.0 Parse (-32700) error.
*/
parse: <T>(arg: EthErrorsArg<T>) => getEthJsonRpcError(
parse: <T>(arg?: EthErrorsArg<T>) => getEthJsonRpcError(
errorCodes.rpc.parse, arg,
),

/**
* Get a JSON RPC 2.0 Invalid Request (-32600) error.
*/
invalidRequest: <T>(arg: EthErrorsArg<T>) => getEthJsonRpcError(
invalidRequest: <T>(arg?: EthErrorsArg<T>) => getEthJsonRpcError(
errorCodes.rpc.invalidRequest, arg,
),

/**
* Get a JSON RPC 2.0 Invalid Params (-32602) error.
*/
invalidParams: <T>(arg: EthErrorsArg<T>) => getEthJsonRpcError(
invalidParams: <T>(arg?: EthErrorsArg<T>) => getEthJsonRpcError(
errorCodes.rpc.invalidParams, arg,
),

/**
* Get a JSON RPC 2.0 Method Not Found (-32601) error.
*/
methodNotFound: <T>(arg: EthErrorsArg<T>) => getEthJsonRpcError(
methodNotFound: <T>(arg?: EthErrorsArg<T>) => getEthJsonRpcError(
errorCodes.rpc.methodNotFound, arg,
),

/**
* Get a JSON RPC 2.0 Internal (-32603) error.
*/
internal: <T>(arg: EthErrorsArg<T>) => getEthJsonRpcError(
internal: <T>(arg?: EthErrorsArg<T>) => getEthJsonRpcError(
errorCodes.rpc.internal, arg,
),

Expand All @@ -74,42 +74,42 @@ export const ethErrors = {
/**
* Get an Ethereum JSON RPC Invalid Input (-32000) error.
*/
invalidInput: <T>(arg: EthErrorsArg<T>) => getEthJsonRpcError(
invalidInput: <T>(arg?: EthErrorsArg<T>) => getEthJsonRpcError(
errorCodes.rpc.invalidInput, arg,
),

/**
* Get an Ethereum JSON RPC Resource Not Found (-32001) error.
*/
resourceNotFound: <T>(arg: EthErrorsArg<T>) => getEthJsonRpcError(
resourceNotFound: <T>(arg?: EthErrorsArg<T>) => getEthJsonRpcError(
errorCodes.rpc.resourceNotFound, arg,
),

/**
* Get an Ethereum JSON RPC Resource Unavailable (-32002) error.
*/
resourceUnavailable: <T>(arg: EthErrorsArg<T>) => getEthJsonRpcError(
resourceUnavailable: <T>(arg?: EthErrorsArg<T>) => getEthJsonRpcError(
errorCodes.rpc.resourceUnavailable, arg,
),

/**
* Get an Ethereum JSON RPC Transaction Rejected (-32003) error.
*/
transactionRejected: <T>(arg: EthErrorsArg<T>) => getEthJsonRpcError(
transactionRejected: <T>(arg?: EthErrorsArg<T>) => getEthJsonRpcError(
errorCodes.rpc.transactionRejected, arg,
),

/**
* Get an Ethereum JSON RPC Method Not Supported (-32004) error.
*/
methodNotSupported: <T>(arg: EthErrorsArg<T>) => getEthJsonRpcError(
methodNotSupported: <T>(arg?: EthErrorsArg<T>) => getEthJsonRpcError(
errorCodes.rpc.methodNotSupported, arg,
),

/**
* Get an Ethereum JSON RPC Limit Exceeded (-32005) error.
*/
limitExceeded: <T>(arg: EthErrorsArg<T>) => getEthJsonRpcError(
limitExceeded: <T>(arg?: EthErrorsArg<T>) => getEthJsonRpcError(
errorCodes.rpc.limitExceeded, arg,
),
},
Expand All @@ -119,7 +119,7 @@ export const ethErrors = {
/**
* Get an Ethereum Provider User Rejected Request (4001) error.
*/
userRejectedRequest: <T>(arg: EthErrorsArg<T>) => {
userRejectedRequest: <T>(arg?: EthErrorsArg<T>) => {
return getEthProviderError(
errorCodes.provider.userRejectedRequest, arg,
);
Expand All @@ -128,7 +128,7 @@ export const ethErrors = {
/**
* Get an Ethereum Provider Unauthorized (4100) error.
*/
unauthorized: <T>(arg: EthErrorsArg<T>) => {
unauthorized: <T>(arg?: EthErrorsArg<T>) => {
return getEthProviderError(
errorCodes.provider.unauthorized, arg,
);
Expand All @@ -137,7 +137,7 @@ export const ethErrors = {
/**
* Get an Ethereum Provider Unsupported Method (4200) error.
*/
unsupportedMethod: <T>(arg: EthErrorsArg<T>) => {
unsupportedMethod: <T>(arg?: EthErrorsArg<T>) => {
return getEthProviderError(
errorCodes.provider.unsupportedMethod, arg,
);
Expand All @@ -146,7 +146,7 @@ export const ethErrors = {
/**
* Get an Ethereum Provider Not Connected (4900) error.
*/
disconnected: <T>(arg: EthErrorsArg<T>) => {
disconnected: <T>(arg?: EthErrorsArg<T>) => {
return getEthProviderError(
errorCodes.provider.disconnected, arg,
);
Expand All @@ -155,7 +155,7 @@ export const ethErrors = {
/**
* Get an Ethereum Provider Chain Not Connected (4901) error.
*/
chainDisconnected: <T>(arg: EthErrorsArg<T>) => {
chainDisconnected: <T>(arg?: EthErrorsArg<T>) => {
return getEthProviderError(
errorCodes.provider.chainDisconnected, arg,
);
Expand All @@ -164,11 +164,13 @@ export const ethErrors = {
/**
* Get a custom Ethereum Provider error.
*/
custom: <T>(opts: CustomErrorOptions<T>) => {
custom: <T>(opts: CustomErrorArg<T>) => {
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',
Expand All @@ -181,7 +183,7 @@ export const ethErrors = {

// Internal

function getEthJsonRpcError<T>(code: number, arg: EthErrorsArg<T>): EthereumRpcError<T> {
function getEthJsonRpcError<T>(code: number, arg?: EthErrorsArg<T>): EthereumRpcError<T> {
const [message, data] = parseOpts(arg);
return new EthereumRpcError(
code,
Expand All @@ -190,7 +192,7 @@ function getEthJsonRpcError<T>(code: number, arg: EthErrorsArg<T>): EthereumRpcE
);
}

function getEthProviderError<T>(code: number, arg: EthErrorsArg<T>): EthereumProviderError<T> {
function getEthProviderError<T>(code: number, arg?: EthErrorsArg<T>): EthereumProviderError<T> {
const [message, data] = parseOpts(arg);
return new EthereumProviderError(
code,
Expand All @@ -199,7 +201,7 @@ function getEthProviderError<T>(code: number, arg: EthErrorsArg<T>): EthereumPro
);
}

function parseOpts<T>(arg: EthErrorsArg<T>): [string?, T?] {
function parseOpts<T>(arg?: EthErrorsArg<T>): [string?, T?] {
if (arg) {
if (typeof arg === 'string') {
return [arg];
Expand Down
10 changes: 10 additions & 0 deletions test/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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();
});
Expand Down

0 comments on commit 22089c1

Please sign in to comment.