Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use AbiError instead of Error for errors at web3-eth-abi #6641

Merged
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
6 changes: 5 additions & 1 deletion packages/web3-eth-abi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,8 @@ Documentation:

- Bug fix of `ERR_UNSUPPORTED_DIR_IMPORT` in ABI (#6535)

## [Unreleased]
## [Unreleased]

### Changed

- Use `AbiError` instead of `Error` for errors at web3-eth-abi (#6641).
11 changes: 9 additions & 2 deletions packages/web3-eth-abi/src/coders/base/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/

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';
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
});
}
15 changes: 12 additions & 3 deletions packages/web3-eth-abi/src/eip_712.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.

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+/;
Expand Down Expand Up @@ -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,
},
);
}

Expand Down Expand Up @@ -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];
Expand Down
37 changes: 37 additions & 0 deletions packages/web3-eth-abi/test/unit/coders/base/invalid.test.ts
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

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);
});
});
});
Loading