Skip to content

Commit

Permalink
Use AbiError instead of Error for errors at web3-eth-abi (#6641)
Browse files Browse the repository at this point in the history
* use AbiError instead of Error for errors at web3-eth-abi

* update CHANGELOG.md

* add unit test
  • Loading branch information
Muhammad-Altabba committed Dec 5, 2023
1 parent b819ee4 commit f1c6916
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 6 deletions.
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 @@ -65,6 +65,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 @@ -182,12 +183,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 @@ -226,7 +232,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);
});
});
});

0 comments on commit f1c6916

Please sign in to comment.