diff --git a/packages/web3-eth-abi/CHANGELOG.md b/packages/web3-eth-abi/CHANGELOG.md
index 69c387b19c7..2192a71e872 100644
--- a/packages/web3-eth-abi/CHANGELOG.md
+++ b/packages/web3-eth-abi/CHANGELOG.md
@@ -154,4 +154,8 @@ Documentation:
- Bug fix of `ERR_UNSUPPORTED_DIR_IMPORT` in ABI (#6535)
-## [Unreleased]
\ No newline at end of file
+## [Unreleased]
+
+### Changed
+
+- Use `AbiError` instead of `Error` for errors at web3-eth-abi (#6641).
diff --git a/packages/web3-eth-abi/src/coders/base/index.ts b/packages/web3-eth-abi/src/coders/base/index.ts
index 007d31e98a4..007e3ebb4d6 100644
--- a/packages/web3-eth-abi/src/coders/base/index.ts
+++ b/packages/web3-eth-abi/src/coders/base/index.ts
@@ -16,6 +16,7 @@ along with web3.js. If not, see .
*/
import { AbiParameter } from 'web3-types';
+import { AbiError } from 'web3-errors';
import { EncoderResult, DecoderResult } from '../types.js';
import { decodeAddress, encodeAddress } from './address.js';
import { decodeBool, encodeBoolean } from './bool.js';
@@ -59,7 +60,10 @@ export function encodeParamFromAbiParameter(param: AbiParameter, value: unknown)
if (param.type.startsWith('uint') || param.type.startsWith('int')) {
return encodeNumber(param, value);
}
- throw new Error('Unsupported');
+ throw new AbiError('Unsupported', {
+ param,
+ value,
+ });
}
export function decodeParamFromAbiParameter(param: AbiParameter, bytes: Uint8Array): DecoderResult {
@@ -84,5 +88,8 @@ export function decodeParamFromAbiParameter(param: AbiParameter, bytes: Uint8Arr
if (param.type.startsWith('uint') || param.type.startsWith('int')) {
return decodeNumber(param, bytes);
}
- throw new Error('Unsupported');
+ throw new AbiError('Unsupported', {
+ param,
+ bytes,
+ });
}
diff --git a/packages/web3-eth-abi/src/eip_712.ts b/packages/web3-eth-abi/src/eip_712.ts
index f08609bffc5..19477261634 100644
--- a/packages/web3-eth-abi/src/eip_712.ts
+++ b/packages/web3-eth-abi/src/eip_712.ts
@@ -21,6 +21,7 @@ along with web3.js. If not, see .
import { Eip712TypedData } from 'web3-types';
import { isNullish, keccak256 } from 'web3-utils';
+import { AbiError } from 'web3-errors';
import { encodeParameters } from './coders/encode.js';
const TYPE_REGEX = /^\w+/;
@@ -138,12 +139,17 @@ const encodeValue = (
const length = Number(match[2]) || undefined;
if (!Array.isArray(data)) {
- throw new Error('Cannot encode data: value is not of array type');
+ throw new AbiError('Cannot encode data: value is not of array type', {
+ data,
+ });
}
if (length && data.length !== length) {
- throw new Error(
+ throw new AbiError(
`Cannot encode data: expected length of ${length}, but got ${data.length}`,
+ {
+ data,
+ },
);
}
@@ -182,7 +188,10 @@ const encodeData = (
const [types, values] = typedData.types[type].reduce<[string[], unknown[]]>(
([_types, _values], field) => {
if (isNullish(data[field.name]) || isNullish(data[field.name])) {
- throw new Error(`Cannot encode data: missing data for '${field.name}'`);
+ throw new AbiError(`Cannot encode data: missing data for '${field.name}'`, {
+ data,
+ field,
+ });
}
const value = data[field.name];
diff --git a/packages/web3-eth-abi/test/unit/coders/base/invalid.test.ts b/packages/web3-eth-abi/test/unit/coders/base/invalid.test.ts
new file mode 100644
index 00000000000..049f80774ee
--- /dev/null
+++ b/packages/web3-eth-abi/test/unit/coders/base/invalid.test.ts
@@ -0,0 +1,37 @@
+/*
+This file is part of web3.js.
+
+web3.js is free software: you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+web3.js is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License
+along with web3.js. If not, see .
+*/
+
+import { AbiError } from 'web3-errors';
+import {
+ decodeParamFromAbiParameter,
+ encodeParamFromAbiParameter,
+} from '../../../../src/coders/base';
+
+describe('abi - coder - base - invalid', () => {
+ describe('invalid type', () => {
+ it('invalid should cause `decodeParamFromAbiParameter` to throw', () => {
+ expect(() =>
+ decodeParamFromAbiParameter({ type: 'invalid', name: '' }, new Uint8Array()),
+ ).toThrow(AbiError);
+ });
+ it('invalid should cause `encodeParamFromAbiParameter` to throw', () => {
+ expect(() =>
+ encodeParamFromAbiParameter({ type: 'invalid', name: '' }, 'something'),
+ ).toThrow(AbiError);
+ });
+ });
+});