Skip to content

Commit

Permalink
feat: increase rpc error information propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
penovicp committed Sep 2, 2024
1 parent 84f9f02 commit abc4340
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 25 deletions.
15 changes: 15 additions & 0 deletions scripts/generateRpcErrorMap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const starknet_api_openrpc = require('starknet_specs/api/starknet_api_openrpc.json');
const starknet_trace_api_openrpc = require('starknet_specs/api/starknet_trace_api_openrpc.json');
const starknet_write_api = require('starknet_specs/api/starknet_write_api.json');

const errors = Object.fromEntries(
Object.entries({
...starknet_trace_api_openrpc.components.errors,
...starknet_write_api.components.errors,
...starknet_api_openrpc.components.errors,
})
.map((e) => [e[0], e[1].code])
.sort((a, b) => a[1] - b[1])
);

console.log(errors);
8 changes: 2 additions & 6 deletions src/channel/rpc_0_6.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NetworkName, StarknetChainId } from '../constants';
import { LibraryError } from '../provider/errors';
import { LibraryError, RpcError } from '../utils/errors';
import {
AccountInvocationItem,
AccountInvocations,
Expand Down Expand Up @@ -102,11 +102,7 @@ export class RpcChannel {

protected errorHandler(method: string, params: any, rpcError?: JRPC.Error, otherError?: any) {
if (rpcError) {
const { code, message, data } = rpcError;
throw new LibraryError(
`RPC: ${method} with params ${stringify(params, null, 2)}\n
${code}: ${message}: ${stringify(data)}`
);
throw new RpcError(rpcError, method, params);
}
if (otherError instanceof LibraryError) {
throw otherError;
Expand Down
8 changes: 2 additions & 6 deletions src/channel/rpc_0_7.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NetworkName, StarknetChainId } from '../constants';
import { LibraryError } from '../provider/errors';
import { LibraryError, RpcError } from '../utils/errors';
import {
AccountInvocationItem,
AccountInvocations,
Expand Down Expand Up @@ -118,11 +118,7 @@ export class RpcChannel {

protected errorHandler(method: string, params: any, rpcError?: JRPC.Error, otherError?: any) {
if (rpcError) {
const { code, message, data } = rpcError;
throw new LibraryError(
`RPC: ${method} with params ${stringify(params, null, 2)}\n
${code}: ${message}: ${stringify(data)}`
);
throw new RpcError(rpcError, method, params);
}
if (otherError instanceof LibraryError) {
throw otherError;
Expand Down
2 changes: 1 addition & 1 deletion src/provider/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { RpcProvider } from './rpc';

export { RpcProvider as Provider } from './extensions/default'; // backward-compatibility
export * from './errors';
export * from '../utils/errors';
export * from './interface';
export * from './extensions/default';

Expand Down
2 changes: 1 addition & 1 deletion src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import { isBigNumberish, toBigInt, toHex } from '../utils/num';
import { wait } from '../utils/provider';
import { RPCResponseParser } from '../utils/responseParser/rpc';
import { GetTransactionReceiptResponse, ReceiptTx } from '../utils/transactionReceipt';
import { LibraryError } from './errors';
import { LibraryError } from '../utils/errors';
import { ProviderInterface } from './interface';
import { solidityUint256PackedKeccak256 } from '../utils/hash';
import { CallData } from '../utils/calldata';
Expand Down
38 changes: 27 additions & 11 deletions src/provider/errors.ts → src/utils/errors/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/* eslint-disable max-classes-per-file */
import { JRPC } from '../../types/api';
import { stringify } from '../json';
import rpcErrors from './rpc';

// eslint-disable-next-line max-classes-per-file
export function fixStack(target: Error, fn: Function = target.constructor) {
const { captureStackTrace } = Error as any;
Expand Down Expand Up @@ -36,20 +41,31 @@ export class CustomError extends Error {

export class LibraryError extends CustomError {}

export class GatewayError extends LibraryError {
export class RpcError extends LibraryError {
// TODO consider property rename and decide best way to set types

public readonly request: {
method: string;
params: any;
};

constructor(
message: string,
public errorCode: string
public readonly original: JRPC.Error,
method: string,
params: any
) {
super(message);
// legacy message format
super(`RPC: ${method} with params ${stringify(params, null, 2)}\n
${original.code}: ${original.message}: ${stringify(original.data)}`);

this.request = { method, params };
}
}

export class HttpError extends LibraryError {
constructor(
message: string,
public errorCode: number
) {
super(message);
public get code() {
return this.original.code;
}

public isType(type: string): boolean {
return rpcErrors[type as keyof typeof rpcErrors] === this.code;
}
}
28 changes: 28 additions & 0 deletions src/utils/errors/rpc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const errors = {
FAILED_TO_RECEIVE_TXN: 1,
CONTRACT_NOT_FOUND: 20,
BLOCK_NOT_FOUND: 24,
INVALID_TXN_INDEX: 27,
CLASS_HASH_NOT_FOUND: 28,
TXN_HASH_NOT_FOUND: 29,
PAGE_SIZE_TOO_BIG: 31,
NO_BLOCKS: 32,
INVALID_CONTINUATION_TOKEN: 33,
TOO_MANY_KEYS_IN_FILTER: 34,
CONTRACT_ERROR: 40,
TRANSACTION_EXECUTION_ERROR: 41,
CLASS_ALREADY_DECLARED: 51,
INVALID_TRANSACTION_NONCE: 52,
INSUFFICIENT_MAX_FEE: 53,
INSUFFICIENT_ACCOUNT_BALANCE: 54,
VALIDATION_FAILURE: 55,
COMPILATION_FAILED: 56,
CONTRACT_CLASS_SIZE_IS_TOO_LARGE: 57,
NON_ACCOUNT: 58,
DUPLICATE_TX: 59,
COMPILED_CLASS_HASH_MISMATCH: 60,
UNSUPPORTED_TX_VERSION: 61,
UNSUPPORTED_CONTRACT_CLASS_VERSION: 62,
UNEXPECTED_ERROR: 63,
};
export default errors;

0 comments on commit abc4340

Please sign in to comment.