Skip to content

Commit 9442806

Browse files
authored
Merge pull request #2 from mahnunchik/code
feat: expose error code
2 parents 48c4b85 + de4767a commit 9442806

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

index.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,16 @@ type NetworkOpts = {
217217
};
218218

219219
type RpcParams = any[] | Record<string, any>;
220-
type RpcError = { code: number; message: string };
220+
type RpcErrorResponse = { code: number; message: string };
221+
222+
export class RpcError extends Error {
223+
readonly code: number;
224+
constructor(error: RpcErrorResponse) {
225+
super(`FetchProvider(${error.code}): ${error.message || error}`);
226+
this.code = error.code;
227+
this.name = 'RpcError';
228+
}
229+
}
221230

222231
/**
223232
* Small utility class for Jsonrpc
@@ -256,8 +265,8 @@ export class JsonrpcProvider implements JsonrpcInterface {
256265
});
257266
return await res.json();
258267
}
259-
private jsonError(error: RpcError) {
260-
return new Error(`FetchProvider(${error.code}): ${error.message || error}`);
268+
private jsonError(error: RpcErrorResponse) {
269+
return new RpcError(error);
261270
}
262271
private async batchProcess() {
263272
await nextTick(); // this allows to collect as much requests as we can in single tick

0 commit comments

Comments
 (0)