Skip to content

Commit

Permalink
refactor: using nested error codes for JSONRPCProviderError classes (
Browse files Browse the repository at this point in the history
…#1403)

* feat: using generic code for json rpc exceptions

* feat: using generic code for json rpc exceptions

* feat: using generic code for json rpc exceptions

* feat: fixed tests
  • Loading branch information
freemanzMrojo authored Oct 9, 2024
1 parent 7ea84ca commit af205a8
Show file tree
Hide file tree
Showing 50 changed files with 93 additions and 128 deletions.
90 changes: 68 additions & 22 deletions packages/errors/src/available-errors/provider/provider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { VechainSDKError } from '../sdk-error';
import { type ObjectErrorData } from '../types';
import type { JSONRpcErrorCode, ObjectErrorData } from '../types';

/**
* Provider generic error.
Expand All @@ -9,22 +9,14 @@ import { type ObjectErrorData } from '../types';
*
* @see{https://www.jsonrpc.org/specification#error_object}
*/
class JSONRPCProviderError<
TJSONRpcErrorCode extends
| -32700
| -32600
| -32601
| -32602
| -32603
| -32000
> extends VechainSDKError<{
code: TJSONRpcErrorCode;
class JSONRPCProviderError extends VechainSDKError<{
code: JSONRpcErrorCode;
message: string;
data: ObjectErrorData;
}> {
constructor(
readonly methodName: string,
code: TJSONRpcErrorCode,
code: JSONRpcErrorCode,
message: string,
data: ObjectErrorData,
readonly innerError?: unknown
Expand All @@ -39,54 +31,108 @@ class JSONRPCProviderError<
* WHEN TO USE:
* * Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text.
*/
class JSONRPCParseError extends JSONRPCProviderError<-32700> {}
class JSONRPCParseError extends JSONRPCProviderError {
constructor(
readonly methodName: string,
message: string,
data: ObjectErrorData,
readonly innerError?: unknown
) {
super(methodName, -32700, message, data, innerError);
}
}

/**
* Invalid request.
*
* WHEN TO USE:
* * The JSON sent is not a valid Request object.
*/
class JSONRPCInvalidRequest extends JSONRPCProviderError<-32600> {}
class JSONRPCInvalidRequest extends JSONRPCProviderError {
constructor(
readonly methodName: string,
message: string,
data: ObjectErrorData,
readonly innerError?: unknown
) {
super(methodName, -32600, message, data, innerError);
}
}

/**
* Method not found.
*
* WHEN TO USE:
* * The method does not exist / is not available.
*/
class JSONRPCMethodNotFound extends JSONRPCProviderError<-32601> {}
class JSONRPCMethodNotFound extends JSONRPCProviderError {
constructor(
readonly methodName: string,
message: string,
data: ObjectErrorData,
readonly innerError?: unknown
) {
super(methodName, -32601, message, data, innerError);
}
}

/**
* Invalid params.
*
* WHEN TO USE:
* * Invalid method parameter(s).
*/
class JSONRPCInvalidParams extends JSONRPCProviderError<-32602> {}
class JSONRPCInvalidParams extends JSONRPCProviderError {
constructor(
readonly methodName: string,
message: string,
data: ObjectErrorData,
readonly innerError?: unknown
) {
super(methodName, -32602, message, data, innerError);
}
}

/**
* Internal JSON-RPC error.
*
* WHEN TO USE:
* * Internal JSON-RPC error.
*/
class JSONRPCInternalError extends JSONRPCProviderError<-32603> {}
class JSONRPCInternalError extends JSONRPCProviderError {
constructor(
readonly methodName: string,
message: string,
data: ObjectErrorData,
readonly innerError?: unknown
) {
super(methodName, -32603, message, data, innerError);
}
}

/**
* Server error.
*
* WHEN TO USE:
* * Reserved for implementation-defined server-errors.
*/
class JSONRPCServerError extends JSONRPCProviderError<-32000> {}
class JSONRPCServerError extends JSONRPCProviderError {
constructor(
readonly methodName: string,
message: string,
data: ObjectErrorData,
readonly innerError?: unknown
) {
super(methodName, -32000, message, data, innerError);
}
}

export {
JSONRPCProviderError,
JSONRPCParseError,
JSONRPCInternalError,
JSONRPCInvalidParams,
JSONRPCInvalidRequest,
JSONRPCMethodNotFound,
JSONRPCInvalidParams,
JSONRPCInternalError,
JSONRPCParseError,
JSONRPCProviderError,
JSONRPCServerError
};
3 changes: 2 additions & 1 deletion packages/errors/src/available-errors/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
* Default Object error data type. it accepts any object.
*/
type ObjectErrorData = Record<string, unknown>;
type JSONRpcErrorCode = -32700 | -32600 | -32601 | -32602 | -32603 | -32000;

export type { ObjectErrorData };
export type { ObjectErrorData, JSONRpcErrorCode };
6 changes: 0 additions & 6 deletions packages/errors/tests/available-errors/provider.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ describe('Error package Available errors test - Provider', () => {
expect(() => {
throw new JSONRPCParseError(
'method',
-32700,
'message',
{ data: 'data' },
innerError
Expand All @@ -41,7 +40,6 @@ describe('Error package Available errors test - Provider', () => {
expect(() => {
throw new JSONRPCInvalidRequest(
'method',
-32600,
'message',
{ data: 'data' },
innerError
Expand All @@ -59,7 +57,6 @@ describe('Error package Available errors test - Provider', () => {
expect(() => {
throw new JSONRPCMethodNotFound(
'method',
-32601,
'message',
{ data: 'data' },
innerError
Expand All @@ -77,7 +74,6 @@ describe('Error package Available errors test - Provider', () => {
expect(() => {
throw new JSONRPCInvalidParams(
'method',
-32602,
'message',
{ data: 'data' },
innerError
Expand All @@ -95,7 +91,6 @@ describe('Error package Available errors test - Provider', () => {
expect(() => {
throw new JSONRPCInternalError(
'method',
-32603,
'message',
{ data: 'data' },
innerError
Expand All @@ -113,7 +108,6 @@ describe('Error package Available errors test - Provider', () => {
expect(() => {
throw new JSONRPCServerError(
'method',
-32000,
'message',
{ data: 'data' },
innerError
Expand Down
1 change: 0 additions & 1 deletion packages/hardhat-plugin/src/helpers/provider-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ const createWalletFromHardhatNetworkConfig = (
if (accountFromConfig === 'remote')
throw new JSONRPCInternalError(
'createWalletFromHardhatNetworkConfig()',
-32603,
'Remote accounts are not supported in hardhat network configuration.',
{ accountFromConfig, networkConfig }
);
Expand Down
11 changes: 3 additions & 8 deletions packages/logging/tests/error-logger.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,9 @@ describe('Error logger tests', () => {
const logSpy = jest.spyOn(VeChainSDKLogger('error'), 'log');

VeChainSDKLogger('error').log(
new JSONRPCInternalError(
'test-method',
-32603,
`Error on request`,
{
some: 'data'
}
)
new JSONRPCInternalError('test-method', `Error on request`, {
some: 'data'
})
);

expect(logSpy).toHaveBeenCalled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ class HardhatVeChainProvider extends VeChainProvider {
VeChainSDKLogger('error').log(
new JSONRPCInternalError(
args.method,
-32603,
`Error on request - ${args.method}`,
{
args
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ class VeChainProvider extends EventEmitter implements EIP1193ProviderMessage {
if (enableDelegation && wallet?.delegator === undefined) {
throw new JSONRPCInvalidParams(
'VechainProvider constructor',
-32602,
'Delegation is enabled but the delegator is not defined. Ensure that the delegator is defined and connected to the network.',
{ wallet }
);
Expand Down Expand Up @@ -99,7 +98,6 @@ class VeChainProvider extends EventEmitter implements EIP1193ProviderMessage {
) {
throw new JSONRPCMethodNotFound(
'VeChainProvider.request()',
-32601,
'Method not found. Invalid RPC method given as input.',
{ method: args.method }
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ const debugTraceBlockByHash = async (
)
throw new JSONRPCInvalidParams(
'debug_traceBlockByHash',
-32602,
`Invalid input params for "debug_traceBlockByHash" method. See https://www.quicknode.com/docs/ethereum/debug_traceBlockByHash for details.`,
{ params }
);
Expand Down Expand Up @@ -86,7 +85,6 @@ const debugTraceBlockByHash = async (
} catch (e) {
throw new JSONRPCInternalError(
'debug_traceBlockByHash()',
-32603,
'Method "debug_traceBlockByHash" failed.',
{
params: stringifyData(params),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ const debugTraceBlockByNumber = async (
)
throw new JSONRPCInvalidParams(
'debug_traceBlockByNumber',
-32602,
`Invalid input params for "debug_traceBlockByNumber" method. See https://www.quicknode.com/docs/ethereum/debug_traceBlockByNumber for details.`,
{ params }
);
Expand Down Expand Up @@ -87,7 +86,6 @@ const debugTraceBlockByNumber = async (
} catch (e) {
throw new JSONRPCInternalError(
'debug_traceBlockByNumber()',
-32603,
'Method "debug_traceBlockByNumber" failed.',
{
params: stringifyData(params),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ const debugTraceCall = async (
)
throw new JSONRPCInvalidParams(
'debug_traceCall',
-32602,
`Invalid input params for "debug_traceCall" method. See ${RPC_DOCUMENTATION_URL} for details.`,
{ params }
);
Expand Down Expand Up @@ -84,7 +83,6 @@ const debugTraceCall = async (
} catch (e) {
throw new JSONRPCInternalError(
'debug_traceCall()',
-32603,
'Method "debug_traceCall" failed.',
{
params: stringifyData(params),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ const debugTraceTransaction = async (
)
throw new JSONRPCInvalidParams(
'debug_traceTransaction',
-32602,
`Invalid input params for "debug_traceTransaction" method. See ${RPC_DOCUMENTATION_URL} for details.`,
{ params }
);
Expand Down Expand Up @@ -87,7 +86,6 @@ const debugTraceTransaction = async (
} catch (e) {
throw new JSONRPCInternalError(
'debug_traceTransaction()',
-32603,
'Method "debug_traceTransaction" failed.',
{
params: stringifyData(params),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const ethBlockNumber = async (thorClient: ThorClient): Promise<string> => {
} catch (e) {
throw new JSONRPCInternalError(
'eth_blockNumber()',
-32603,
'Method "eth_blockNumber" failed.',
{
url: thorClient.httpClient.baseURL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const ethCall = async (
)
throw new JSONRPCInvalidParams(
'eth_call',
-32602,
`Invalid input params for "eth_call" method. See ${RPC_DOCUMENTATION_URL} for details.`,
{ params }
);
Expand Down Expand Up @@ -70,7 +69,6 @@ const ethCall = async (
} catch (e) {
throw new JSONRPCInternalError(
'eth_call()',
-32603,
'Method "eth_call" failed.',
{
params: stringifyData(params),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const ethChainId = async (thorClient: ThorClient): Promise<string> => {
if (genesisBlock?.id === null || genesisBlock?.id === undefined) {
throw new JSONRPCInvalidParams(
'eth_chainId()',
-32602,
'The genesis block id is null or undefined. Unable to get the chain id.',
{
url: thorClient.httpClient.baseURL
Expand All @@ -41,7 +40,6 @@ const ethChainId = async (thorClient: ThorClient): Promise<string> => {
} catch (e) {
throw new JSONRPCInternalError(
'eth_chainId()',
-32603,
'Method "eth_chainId" failed.',
{
url: thorClient.httpClient.baseURL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const ethEstimateGas = async (
if (![1, 2].includes(params.length) || typeof params[0] !== 'object')
throw new JSONRPCInvalidParams(
'eth_estimateGas',
-32602,
`Invalid input params for "eth_estimateGas" method. See ${RPC_DOCUMENTATION_URL} for details.`,
{ params }
);
Expand Down Expand Up @@ -70,7 +69,6 @@ const ethEstimateGas = async (
} catch (e) {
throw new JSONRPCInternalError(
'eth_estimateGas()',
-32603,
'Method "eth_estimateGas" failed.',
{
params: stringifyData(params),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const ethGetBalance = async (
)
throw new JSONRPCInvalidParams(
'eth_getBalance',
-32602,
`Invalid input params for "eth_getBalance" method. See ${RPC_DOCUMENTATION_URL} for details.`,
{ params }
);
Expand All @@ -51,7 +50,6 @@ const ethGetBalance = async (
} catch (e) {
throw new JSONRPCInternalError(
'eth_getBalance()',
-32603,
'Method "eth_getBalance" failed.',
{
params: stringifyData(params),
Expand Down
Loading

1 comment on commit af205a8

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test Coverage

Summary

Lines Statements Branches Functions
Coverage: 99%
99.08% (4208/4247) 97.74% (1388/1420) 99.09% (876/884)
Title Tests Skipped Failures Errors Time
core 799 0 💤 0 ❌ 0 🔥 1m 48s ⏱️
network 735 0 💤 0 ❌ 0 🔥 4m 37s ⏱️
errors 42 0 💤 0 ❌ 0 🔥 16.388s ⏱️

Please sign in to comment.