diff --git a/packages/web3-errors/src/errors/contract_errors.ts b/packages/web3-errors/src/errors/contract_errors.ts index 3710d9f60f0..ccd2b7dd46a 100644 --- a/packages/web3-errors/src/errors/contract_errors.ts +++ b/packages/web3-errors/src/errors/contract_errors.ts @@ -159,7 +159,7 @@ export class Eip838ExecutionError extends Web3ContractError { // error.data, error.data.data or error.data.originalError.data (https://github.com/web3/web3.js/issues/4454#issuecomment-1485953455) if (typeof error.data === 'object') { let originalError: { data: string }; - if ('originalError' in error.data) { + if (error.data && 'originalError' in error.data) { originalError = error.data.originalError; } else { // Ganache has no `originalError` sub-object unlike others diff --git a/packages/web3-errors/test/unit/__snapshots__/errors.test.ts.snap b/packages/web3-errors/test/unit/__snapshots__/errors.test.ts.snap index 2eefbaa0814..22a863812e8 100644 --- a/packages/web3-errors/test/unit/__snapshots__/errors.test.ts.snap +++ b/packages/web3-errors/test/unit/__snapshots__/errors.test.ts.snap @@ -156,6 +156,17 @@ exports[`errors Eip838ExecutionError should get the data from error.data.origina } `; +exports[`errors Eip838ExecutionError should return correctly when data is undefined 1`] = ` +{ + "cause": undefined, + "code": undefined, + "data": undefined, + "innerError": undefined, + "message": "Error", + "name": "Eip838ExecutionError", +} +`; + exports[`errors InvalidConnectionError should have valid json structure 1`] = ` { "cause": undefined, diff --git a/packages/web3-errors/test/unit/errors.test.ts b/packages/web3-errors/test/unit/errors.test.ts index 7428cc9b6b3..0731f4dff37 100644 --- a/packages/web3-errors/test/unit/errors.test.ts +++ b/packages/web3-errors/test/unit/errors.test.ts @@ -329,6 +329,13 @@ describe('errors', () => { } as JsonRpcError).toJSON(), ).toMatchSnapshot(); }); + it('should return correctly when data is undefined', () => { + expect( + new contractErrors.Eip838ExecutionError({ + data: undefined, + } as JsonRpcError).toJSON(), + ).toMatchSnapshot(); + }); }); describe('ResponseError', () => {