Skip to content

Commit df1102a

Browse files
committed
Expand use of OptionalDataWithOptionalCause
1 parent c5e053f commit df1102a

File tree

3 files changed

+43
-37
lines changed

3 files changed

+43
-37
lines changed

jest.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ module.exports = {
4747
global: {
4848
branches: 94.31,
4949
functions: 94.11,
50-
lines: 97.11,
51-
statements: 97.11,
50+
lines: 97.13,
51+
statements: 97.13,
5252
},
5353
},
5454

src/classes.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ import {
55
} from '@metamask/utils';
66
import safeStringify from 'fast-safe-stringify';
77

8-
import type {
9-
DataWithOptionalCause,
10-
OptionalDataWithOptionalCause,
11-
} from './utils';
8+
import type { OptionalDataWithOptionalCause } from './utils';
129
import { serializeCause } from './utils';
1310

1411
export type { SerializedJsonRpcError };
@@ -87,7 +84,7 @@ export class JsonRpcError<
8784
* Permits integer error codes in the [ 1000 <= 4999 ] range.
8885
*/
8986
export class EthereumProviderError<
90-
T extends DataWithOptionalCause,
87+
T extends OptionalDataWithOptionalCause,
9188
> extends JsonRpcError<T> {
9289
/**
9390
* Create an Ethereum Provider JSON-RPC error.

src/errors.ts

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import { JsonRpcError, EthereumProviderError } from './classes';
22
import { errorCodes } from './error-constants';
3-
import { DataWithOptionalCause, getMessageFromCode } from './utils';
3+
import { OptionalDataWithOptionalCause, getMessageFromCode } from './utils';
44

5-
type EthereumErrorOptions<T extends DataWithOptionalCause> = {
5+
type EthereumErrorOptions<T extends OptionalDataWithOptionalCause> = {
66
message?: string;
77
data?: T;
88
};
99

10-
type ServerErrorOptions<T extends DataWithOptionalCause> = {
10+
type ServerErrorOptions<T extends OptionalDataWithOptionalCause> = {
1111
code: number;
1212
} & EthereumErrorOptions<T>;
1313

14-
type CustomErrorArg<T extends DataWithOptionalCause> = ServerErrorOptions<T>;
14+
type CustomErrorArg<T extends OptionalDataWithOptionalCause> =
15+
ServerErrorOptions<T>;
1516

16-
type JsonRpcErrorsArg<T extends DataWithOptionalCause> =
17+
type JsonRpcErrorsArg<T extends OptionalDataWithOptionalCause> =
1718
| EthereumErrorOptions<T>
1819
| string;
1920

@@ -24,7 +25,7 @@ export const rpcErrors = {
2425
* @param arg - The error message or options bag.
2526
* @returns An instance of the {@link JsonRpcError} class.
2627
*/
27-
parse: <T extends DataWithOptionalCause>(arg?: JsonRpcErrorsArg<T>) =>
28+
parse: <T extends OptionalDataWithOptionalCause>(arg?: JsonRpcErrorsArg<T>) =>
2829
getJsonRpcError(errorCodes.rpc.parse, arg),
2930

3031
/**
@@ -33,7 +34,7 @@ export const rpcErrors = {
3334
* @param arg - The error message or options bag.
3435
* @returns An instance of the {@link JsonRpcError} class.
3536
*/
36-
invalidRequest: <T extends DataWithOptionalCause>(
37+
invalidRequest: <T extends OptionalDataWithOptionalCause>(
3738
arg?: JsonRpcErrorsArg<T>,
3839
) => getJsonRpcError(errorCodes.rpc.invalidRequest, arg),
3940

@@ -43,16 +44,17 @@ export const rpcErrors = {
4344
* @param arg - The error message or options bag.
4445
* @returns An instance of the {@link JsonRpcError} class.
4546
*/
46-
invalidParams: <T extends DataWithOptionalCause>(arg?: JsonRpcErrorsArg<T>) =>
47-
getJsonRpcError(errorCodes.rpc.invalidParams, arg),
47+
invalidParams: <T extends OptionalDataWithOptionalCause>(
48+
arg?: JsonRpcErrorsArg<T>,
49+
) => getJsonRpcError(errorCodes.rpc.invalidParams, arg),
4850

4951
/**
5052
* Get a JSON RPC 2.0 Method Not Found (-32601) error.
5153
*
5254
* @param arg - The error message or options bag.
5355
* @returns An instance of the {@link JsonRpcError} class.
5456
*/
55-
methodNotFound: <T extends DataWithOptionalCause>(
57+
methodNotFound: <T extends OptionalDataWithOptionalCause>(
5658
arg?: JsonRpcErrorsArg<T>,
5759
) => getJsonRpcError(errorCodes.rpc.methodNotFound, arg),
5860

@@ -62,8 +64,9 @@ export const rpcErrors = {
6264
* @param arg - The error message or options bag.
6365
* @returns An instance of the {@link JsonRpcError} class.
6466
*/
65-
internal: <T extends DataWithOptionalCause>(arg?: JsonRpcErrorsArg<T>) =>
66-
getJsonRpcError(errorCodes.rpc.internal, arg),
67+
internal: <T extends OptionalDataWithOptionalCause>(
68+
arg?: JsonRpcErrorsArg<T>,
69+
) => getJsonRpcError(errorCodes.rpc.internal, arg),
6770

6871
/**
6972
* Get a JSON RPC 2.0 Server error.
@@ -73,7 +76,9 @@ export const rpcErrors = {
7376
* @param opts - The error options bag.
7477
* @returns An instance of the {@link JsonRpcError} class.
7578
*/
76-
server: <T extends DataWithOptionalCause>(opts: ServerErrorOptions<T>) => {
79+
server: <T extends OptionalDataWithOptionalCause>(
80+
opts: ServerErrorOptions<T>,
81+
) => {
7782
if (!opts || typeof opts !== 'object' || Array.isArray(opts)) {
7883
throw new Error(
7984
'Ethereum RPC Server errors must provide single object argument.',
@@ -94,16 +99,17 @@ export const rpcErrors = {
9499
* @param arg - The error message or options bag.
95100
* @returns An instance of the {@link JsonRpcError} class.
96101
*/
97-
invalidInput: <T extends DataWithOptionalCause>(arg?: JsonRpcErrorsArg<T>) =>
98-
getJsonRpcError(errorCodes.rpc.invalidInput, arg),
102+
invalidInput: <T extends OptionalDataWithOptionalCause>(
103+
arg?: JsonRpcErrorsArg<T>,
104+
) => getJsonRpcError(errorCodes.rpc.invalidInput, arg),
99105

100106
/**
101107
* Get an Ethereum JSON RPC Resource Not Found (-32001) error.
102108
*
103109
* @param arg - The error message or options bag.
104110
* @returns An instance of the {@link JsonRpcError} class.
105111
*/
106-
resourceNotFound: <T extends DataWithOptionalCause>(
112+
resourceNotFound: <T extends OptionalDataWithOptionalCause>(
107113
arg?: JsonRpcErrorsArg<T>,
108114
) => getJsonRpcError(errorCodes.rpc.resourceNotFound, arg),
109115

@@ -113,7 +119,7 @@ export const rpcErrors = {
113119
* @param arg - The error message or options bag.
114120
* @returns An instance of the {@link JsonRpcError} class.
115121
*/
116-
resourceUnavailable: <T extends DataWithOptionalCause>(
122+
resourceUnavailable: <T extends OptionalDataWithOptionalCause>(
117123
arg?: JsonRpcErrorsArg<T>,
118124
) => getJsonRpcError(errorCodes.rpc.resourceUnavailable, arg),
119125

@@ -123,7 +129,7 @@ export const rpcErrors = {
123129
* @param arg - The error message or options bag.
124130
* @returns An instance of the {@link JsonRpcError} class.
125131
*/
126-
transactionRejected: <T extends DataWithOptionalCause>(
132+
transactionRejected: <T extends OptionalDataWithOptionalCause>(
127133
arg?: JsonRpcErrorsArg<T>,
128134
) => getJsonRpcError(errorCodes.rpc.transactionRejected, arg),
129135

@@ -133,7 +139,7 @@ export const rpcErrors = {
133139
* @param arg - The error message or options bag.
134140
* @returns An instance of the {@link JsonRpcError} class.
135141
*/
136-
methodNotSupported: <T extends DataWithOptionalCause>(
142+
methodNotSupported: <T extends OptionalDataWithOptionalCause>(
137143
arg?: JsonRpcErrorsArg<T>,
138144
) => getJsonRpcError(errorCodes.rpc.methodNotSupported, arg),
139145

@@ -143,8 +149,9 @@ export const rpcErrors = {
143149
* @param arg - The error message or options bag.
144150
* @returns An instance of the {@link JsonRpcError} class.
145151
*/
146-
limitExceeded: <T extends DataWithOptionalCause>(arg?: JsonRpcErrorsArg<T>) =>
147-
getJsonRpcError(errorCodes.rpc.limitExceeded, arg),
152+
limitExceeded: <T extends OptionalDataWithOptionalCause>(
153+
arg?: JsonRpcErrorsArg<T>,
154+
) => getJsonRpcError(errorCodes.rpc.limitExceeded, arg),
148155
};
149156

150157
export const providerErrors = {
@@ -154,7 +161,7 @@ export const providerErrors = {
154161
* @param arg - The error message or options bag.
155162
* @returns An instance of the {@link EthereumProviderError} class.
156163
*/
157-
userRejectedRequest: <T extends DataWithOptionalCause>(
164+
userRejectedRequest: <T extends OptionalDataWithOptionalCause>(
158165
arg?: JsonRpcErrorsArg<T>,
159166
) => {
160167
return getEthProviderError(errorCodes.provider.userRejectedRequest, arg);
@@ -166,7 +173,7 @@ export const providerErrors = {
166173
* @param arg - The error message or options bag.
167174
* @returns An instance of the {@link EthereumProviderError} class.
168175
*/
169-
unauthorized: <T extends DataWithOptionalCause>(
176+
unauthorized: <T extends OptionalDataWithOptionalCause>(
170177
arg?: JsonRpcErrorsArg<T>,
171178
) => {
172179
return getEthProviderError(errorCodes.provider.unauthorized, arg);
@@ -178,7 +185,7 @@ export const providerErrors = {
178185
* @param arg - The error message or options bag.
179186
* @returns An instance of the {@link EthereumProviderError} class.
180187
*/
181-
unsupportedMethod: <T extends DataWithOptionalCause>(
188+
unsupportedMethod: <T extends OptionalDataWithOptionalCause>(
182189
arg?: JsonRpcErrorsArg<T>,
183190
) => {
184191
return getEthProviderError(errorCodes.provider.unsupportedMethod, arg);
@@ -190,7 +197,7 @@ export const providerErrors = {
190197
* @param arg - The error message or options bag.
191198
* @returns An instance of the {@link EthereumProviderError} class.
192199
*/
193-
disconnected: <T extends DataWithOptionalCause>(
200+
disconnected: <T extends OptionalDataWithOptionalCause>(
194201
arg?: JsonRpcErrorsArg<T>,
195202
) => {
196203
return getEthProviderError(errorCodes.provider.disconnected, arg);
@@ -202,7 +209,7 @@ export const providerErrors = {
202209
* @param arg - The error message or options bag.
203210
* @returns An instance of the {@link EthereumProviderError} class.
204211
*/
205-
chainDisconnected: <T extends DataWithOptionalCause>(
212+
chainDisconnected: <T extends OptionalDataWithOptionalCause>(
206213
arg?: JsonRpcErrorsArg<T>,
207214
) => {
208215
return getEthProviderError(errorCodes.provider.chainDisconnected, arg);
@@ -214,7 +221,9 @@ export const providerErrors = {
214221
* @param opts - The error options bag.
215222
* @returns An instance of the {@link EthereumProviderError} class.
216223
*/
217-
custom: <T extends DataWithOptionalCause>(opts: CustomErrorArg<T>) => {
224+
custom: <T extends OptionalDataWithOptionalCause>(
225+
opts: CustomErrorArg<T>,
226+
) => {
218227
if (!opts || typeof opts !== 'object' || Array.isArray(opts)) {
219228
throw new Error(
220229
'Ethereum Provider custom errors must provide single object argument.',
@@ -237,7 +246,7 @@ export const providerErrors = {
237246
* @param arg - The error message or options bag.
238247
* @returns An instance of the {@link JsonRpcError} class.
239248
*/
240-
function getJsonRpcError<T extends DataWithOptionalCause>(
249+
function getJsonRpcError<T extends OptionalDataWithOptionalCause>(
241250
code: number,
242251
arg?: JsonRpcErrorsArg<T>,
243252
): JsonRpcError<T> {
@@ -252,7 +261,7 @@ function getJsonRpcError<T extends DataWithOptionalCause>(
252261
* @param arg - The error message or options bag.
253262
* @returns An instance of the {@link EthereumProviderError} class.
254263
*/
255-
function getEthProviderError<T extends DataWithOptionalCause>(
264+
function getEthProviderError<T extends OptionalDataWithOptionalCause>(
256265
code: number,
257266
arg?: JsonRpcErrorsArg<T>,
258267
): EthereumProviderError<T> {
@@ -270,7 +279,7 @@ function getEthProviderError<T extends DataWithOptionalCause>(
270279
* @param arg - The error message or options bag.
271280
* @returns A tuple containing the error message and optional data.
272281
*/
273-
function parseOpts<T extends DataWithOptionalCause>(
282+
function parseOpts<T extends OptionalDataWithOptionalCause>(
274283
arg?: JsonRpcErrorsArg<T>,
275284
): [message?: string | undefined, data?: T | undefined] {
276285
if (arg) {

0 commit comments

Comments
 (0)