Skip to content

Commit

Permalink
Extract the stack trace capture code into @solana/errors (#2291)
Browse files Browse the repository at this point in the history
# Summary

This is used in enough places now that I feel it necessary to extract it into `@solana/errors`. This utility calls `Error.captureStackTrace()` for you, but only if it exists in the runtime.
  • Loading branch information
steveluscher authored Mar 12, 2024
1 parent 52a5d3d commit 273bc9e
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 14 deletions.
1 change: 1 addition & 0 deletions packages/errors/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export * from './codes';
export * from './error';
export * from './json-rpc-error';
export * from './instruction-error';
export * from './stack-trace';
export * from './transaction-error';
5 changes: 2 additions & 3 deletions packages/errors/src/json-rpc-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from './codes';
import { SolanaErrorContext } from './context';
import { SolanaError } from './error';
import { safeCaptureStackTrace } from './stack-trace';
import { getSolanaErrorFromTransactionError } from './transaction-error';

interface RpcErrorResponse {
Expand Down Expand Up @@ -126,8 +127,6 @@ export function getSolanaErrorFromJsonRpcError({ code, data, message }: RpcError
}
out = new SolanaError(code as SolanaErrorCode, errorContext as SolanaErrorContext[SolanaErrorCode]);
}
if ('captureStackTrace' in Error && typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(out, getSolanaErrorFromJsonRpcError);
}
safeCaptureStackTrace(out, getSolanaErrorFromJsonRpcError);
return out;
}
5 changes: 2 additions & 3 deletions packages/errors/src/rpc-enum-errors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SolanaErrorCode } from './codes';
import { SolanaErrorContext } from './context';
import { SolanaError } from './error';
import { safeCaptureStackTrace } from './stack-trace';

type Config = Readonly<{
/**
Expand Down Expand Up @@ -47,8 +48,6 @@ export function getSolanaErrorFromRpcError(
const errorCode = (errorCodeBaseOffset + codeOffset) as SolanaErrorCode;
const errorContext = getErrorContext(errorCode, rpcErrorName, rpcErrorContext);
const err = new SolanaError(errorCode, errorContext);
if ('captureStackTrace' in Error && typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(err, constructorOpt);
}
safeCaptureStackTrace(err, constructorOpt);
return err;
}
5 changes: 5 additions & 0 deletions packages/errors/src/stack-trace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function safeCaptureStackTrace(...args: Parameters<typeof Error.captureStackTrace>): void {
if ('captureStackTrace' in Error && typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(...args);
}
}
6 changes: 2 additions & 4 deletions packages/rpc-subscriptions/src/rpc-integer-overflow-error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SOLANA_ERROR__RPC__INTEGER_OVERFLOW, SolanaError } from '@solana/errors';
import { safeCaptureStackTrace, SOLANA_ERROR__RPC__INTEGER_OVERFLOW, SolanaError } from '@solana/errors';
import type { KeyPath } from '@solana/rpc-transformers';

export function createSolanaJsonRpcIntegerOverflowError(
Expand Down Expand Up @@ -38,8 +38,6 @@ export function createSolanaJsonRpcIntegerOverflowError(
value,
...(path !== undefined ? { path } : undefined),
});
if ('captureStackTrace' in Error && typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(error, createSolanaJsonRpcIntegerOverflowError);
}
safeCaptureStackTrace(error, createSolanaJsonRpcIntegerOverflowError);
return error;
}
6 changes: 2 additions & 4 deletions packages/rpc/src/rpc-integer-overflow-error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SOLANA_ERROR__RPC__INTEGER_OVERFLOW, SolanaError } from '@solana/errors';
import { safeCaptureStackTrace, SOLANA_ERROR__RPC__INTEGER_OVERFLOW, SolanaError } from '@solana/errors';
import type { KeyPath } from '@solana/rpc-transformers';

export function createSolanaJsonRpcIntegerOverflowError(
Expand Down Expand Up @@ -38,8 +38,6 @@ export function createSolanaJsonRpcIntegerOverflowError(
value,
...(path !== undefined ? { path } : undefined),
});
if ('captureStackTrace' in Error && typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(error, createSolanaJsonRpcIntegerOverflowError);
}
safeCaptureStackTrace(error, createSolanaJsonRpcIntegerOverflowError);
return error;
}

0 comments on commit 273bc9e

Please sign in to comment.