Skip to content

Commit

Permalink
Improve rpc client errors processing
Browse files Browse the repository at this point in the history
  • Loading branch information
Comp0te committed Dec 12, 2024
1 parent c02aee3 commit 63f9d02
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
14 changes: 10 additions & 4 deletions src/rpc/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,30 @@ export class RpcError extends Error {
}

@jsonObject
export class HttpError extends Error {
export class HttpError<T extends Error = Error> extends Error {
@jsonMember({ constructor: Error })
sourceErr: Error;
sourceErr: T;

@jsonMember({ constructor: Number })
statusCode: number;

constructor(statusCode = 0, sourceErr: Error = new Error()) {
constructor(statusCode = 0, sourceErr: T) {
super(`Code: ${statusCode}, err: ${sourceErr.message}`);
this.sourceErr = sourceErr;
this.statusCode = statusCode;
}

unwrap(): Error {
unwrap(): T {
return this.sourceErr;
}

isNotFound(): boolean {
return this.statusCode === 404;
}

static isHttpError<E extends Error = Error>(
err: any | HttpError<E>
): err is HttpError<E> {
return err?.statusCode && err.sourceErr;
}
}
9 changes: 3 additions & 6 deletions src/rpc/rpc_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import {
Hash,
Transaction
} from '../types';
import { HttpError } from './error';

export class RpcClient implements IClient {
private handler: IHandler;
Expand Down Expand Up @@ -1283,13 +1284,9 @@ export class RpcClient implements IClient {
const resp = await this.handler.processCall(request);

if (resp.error) {
throw new Error(`RPC call failed, details: ${resp.error.message}`);
throw new HttpError(resp.error.code, resp.error);
}

try {
return resp;
} catch (err) {
throw new Error(`Error parsing result: ${err.message}`);
}
return resp;
}
}

0 comments on commit 63f9d02

Please sign in to comment.