diff --git a/packages/errors/src/index.ts b/packages/errors/src/index.ts index bf5f65366b0e..9498a2f377f6 100644 --- a/packages/errors/src/index.ts +++ b/packages/errors/src/index.ts @@ -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'; diff --git a/packages/errors/src/json-rpc-error.ts b/packages/errors/src/json-rpc-error.ts index 729d55b1a039..2f98c694b7f4 100644 --- a/packages/errors/src/json-rpc-error.ts +++ b/packages/errors/src/json-rpc-error.ts @@ -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 { @@ -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; } diff --git a/packages/errors/src/rpc-enum-errors.ts b/packages/errors/src/rpc-enum-errors.ts index 8e9255fdb14c..d10b2255f9f0 100644 --- a/packages/errors/src/rpc-enum-errors.ts +++ b/packages/errors/src/rpc-enum-errors.ts @@ -1,6 +1,7 @@ import { SolanaErrorCode } from './codes'; import { SolanaErrorContext } from './context'; import { SolanaError } from './error'; +import { safeCaptureStackTrace } from './stack-trace'; type Config = Readonly<{ /** @@ -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; } diff --git a/packages/errors/src/stack-trace.ts b/packages/errors/src/stack-trace.ts new file mode 100644 index 000000000000..e3520bc2ba46 --- /dev/null +++ b/packages/errors/src/stack-trace.ts @@ -0,0 +1,5 @@ +export function safeCaptureStackTrace(...args: Parameters): void { + if ('captureStackTrace' in Error && typeof Error.captureStackTrace === 'function') { + Error.captureStackTrace(...args); + } +} diff --git a/packages/rpc-subscriptions/src/rpc-integer-overflow-error.ts b/packages/rpc-subscriptions/src/rpc-integer-overflow-error.ts index aa454cd08ccb..2b60152d0ab3 100644 --- a/packages/rpc-subscriptions/src/rpc-integer-overflow-error.ts +++ b/packages/rpc-subscriptions/src/rpc-integer-overflow-error.ts @@ -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( @@ -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; } diff --git a/packages/rpc/src/rpc-integer-overflow-error.ts b/packages/rpc/src/rpc-integer-overflow-error.ts index aa454cd08ccb..2b60152d0ab3 100644 --- a/packages/rpc/src/rpc-integer-overflow-error.ts +++ b/packages/rpc/src/rpc-integer-overflow-error.ts @@ -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( @@ -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; }