Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/errors/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,18 @@ export class ContractFunctionRevertedError extends BaseError {

constructor({
abi,
cause,
data,
functionName,
message,
}: {
abi: Abi
cause?: Error | undefined
data?: Hex | undefined
functionName: string
message?: string | undefined
}) {
let cause: Error | undefined
let cause_ = cause
let decodedData: DecodeErrorResultReturnType | undefined
let metaMessages: string[] | undefined
let reason: string | undefined
Expand Down Expand Up @@ -222,13 +224,13 @@ export class ContractFunctionRevertedError extends BaseError {
]
}
} catch (err) {
cause = err as Error
cause_ = err as Error
}
} else if (message) reason = message

let signature: Hex | undefined
if (cause instanceof AbiErrorSignatureNotFoundError) {
signature = cause.signature
if (cause_ instanceof AbiErrorSignatureNotFoundError) {
signature = cause_.signature
metaMessages = [
`Unable to decode signature "${signature}" as it was not found on the provided ABI.`,
'Make sure you are using the correct ABI and that the error exists on it.',
Expand All @@ -246,7 +248,7 @@ export class ContractFunctionRevertedError extends BaseError {
].join('\n')
: `The contract function "${functionName}" reverted.`,
{
cause,
cause: cause_,
metaMessages,
name: 'ContractFunctionRevertedError',
},
Expand Down
22 changes: 22 additions & 0 deletions src/utils/errors/getContractError.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,28 @@ describe('getContractError', () => {
`)
})

test('preserves provider error as nested cause', () => {
const providerError = new Error('Execution error in custom provider') as Error & {
code: number
data: string
name: string
}
providerError.name = 'CustomProviderExecutionError'
providerError.code = 3
providerError.data =
'0x08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000087265766572746564000000000000000000000000000000000000000000000000'

const error = getContractError(new BaseError('An RPC error occurred', { cause: providerError }), {
abi: baycContractConfig.abi,
functionName: 'mintApe',
args: [1n],
sender: accounts[0].address,
})

expect(error.cause.name).toBe('ContractFunctionRevertedError')
expect(error.cause.cause?.name).toBe('CustomProviderExecutionError')
})

test('unknown function', () => {
const error = getContractError(
new BaseError('An RPC error occurred', {
Expand Down
1 change: 1 addition & 0 deletions src/utils/errors/getContractError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export function getContractError<err extends ErrorType<string>>(
) {
return new ContractFunctionRevertedError({
abi,
cause: error,
data: typeof data === 'object' ? data.data : data,
functionName,
message:
Expand Down