Skip to content

Commit 531990c

Browse files
authored
fix: throw custom message on hex validation (#107)
* throw custom message on hex validation * update toHexOrThrow with standardized error messages and make sure parameterName is defined in all usages * simplify error message
1 parent de550c0 commit 531990c

1 file changed

Lines changed: 21 additions & 35 deletions

File tree

packages/smart-accounts-kit/src/actions/erc7715RequestExecutionPermissionsAction.ts

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -256,34 +256,38 @@ function isDefined<TValue>(value: TValue | null | undefined): value is TValue {
256256
* Asserts that a value is defined (not null or undefined).
257257
*
258258
* @param value - The value to check.
259-
* @param message - Optional custom error message to throw if the value is not defined.
259+
* @param parameterName - Optional: The name of the parameter that is being checked.
260260
* @throws {Error} If the value is null or undefined.
261261
*/
262262
function assertIsDefined<TValue>(
263263
value: TValue | null | undefined,
264-
message?: string,
264+
parameterName?: string,
265265
): asserts value is TValue {
266266
if (!isDefined(value)) {
267-
throw new Error(message ?? 'Invalid parameters: value is required');
267+
throw new Error(
268+
`Invalid parameters: ${parameterName ?? 'value'} is required`,
269+
);
268270
}
269271
}
270272

271273
/**
272274
* Converts a value to a hex string or throws an error if the value is invalid.
273275
*
274276
* @param value - The value to convert to hex.
275-
* @param message - Optional custom error message.
277+
* @param parameterName - Optional: The name of the parameter that is being converted to hex.
276278
* @returns The value as a hex string.
277279
*/
278280
function toHexOrThrow(
279281
value: Parameters<typeof toHex>[0] | undefined,
280-
message?: string,
282+
parameterName?: string,
281283
) {
282-
assertIsDefined(value, message);
284+
assertIsDefined(value, parameterName);
283285

284286
if (typeof value === 'string') {
285287
if (!isHex(value)) {
286-
throw new Error('Invalid parameters: invalid hex value');
288+
throw new Error(
289+
`Invalid parameters: ${parameterName ?? 'value'} is not a valid hex value`,
290+
);
287291
}
288292
return value;
289293
}
@@ -361,10 +365,10 @@ function formatNativeTokenStreamPermission({
361365

362366
const optionalFields = {
363367
...(isDefined(initialAmount) && {
364-
initialAmount: toHexOrThrow(initialAmount),
368+
initialAmount: toHexOrThrow(initialAmount, 'initialAmount'),
365369
}),
366370
...(isDefined(maxAmount) && {
367-
maxAmount: toHexOrThrow(maxAmount),
371+
maxAmount: toHexOrThrow(maxAmount, 'maxAmount'),
368372
}),
369373
...(isDefined(startTime) && {
370374
startTime: Number(startTime),
@@ -375,10 +379,7 @@ function formatNativeTokenStreamPermission({
375379
return {
376380
type: 'native-token-stream',
377381
data: {
378-
amountPerSecond: toHexOrThrow(
379-
amountPerSecond,
380-
'Invalid parameters: amountPerSecond is required',
381-
),
382+
amountPerSecond: toHexOrThrow(amountPerSecond, 'amountPerSecond'),
382383
...optionalFields,
383384
},
384385
isAdjustmentAllowed,
@@ -415,10 +416,10 @@ function formatErc20TokenStreamPermission({
415416

416417
const optionalFields = {
417418
...(isDefined(initialAmount) && {
418-
initialAmount: toHexOrThrow(initialAmount),
419+
initialAmount: toHexOrThrow(initialAmount, 'initialAmount'),
419420
}),
420421
...(isDefined(maxAmount) && {
421-
maxAmount: toHexOrThrow(maxAmount),
422+
maxAmount: toHexOrThrow(maxAmount, 'maxAmount'),
422423
}),
423424
...(isDefined(startTime) && {
424425
startTime: Number(startTime),
@@ -429,14 +430,8 @@ function formatErc20TokenStreamPermission({
429430
return {
430431
type: 'erc20-token-stream',
431432
data: {
432-
tokenAddress: toHexOrThrow(
433-
tokenAddress,
434-
'Invalid parameters: tokenAddress is required',
435-
),
436-
amountPerSecond: toHexOrThrow(
437-
amountPerSecond,
438-
'Invalid parameters: amountPerSecond is required',
439-
),
433+
tokenAddress: toHexOrThrow(tokenAddress, 'tokenAddress'),
434+
amountPerSecond: toHexOrThrow(amountPerSecond, 'amountPerSecond'),
440435
...optionalFields,
441436
},
442437
isAdjustmentAllowed,
@@ -472,10 +467,7 @@ function formatNativeTokenPeriodicPermission({
472467
return {
473468
type: 'native-token-periodic',
474469
data: {
475-
periodAmount: toHexOrThrow(
476-
periodAmount,
477-
'Invalid parameters: periodAmount is required',
478-
),
470+
periodAmount: toHexOrThrow(periodAmount, 'periodAmount'),
479471
periodDuration: Number(periodDuration),
480472
...optionalFields,
481473
},
@@ -518,14 +510,8 @@ function formatErc20TokenPeriodicPermission({
518510
return {
519511
type: 'erc20-token-periodic',
520512
data: {
521-
tokenAddress: toHexOrThrow(
522-
tokenAddress,
523-
'Invalid parameters: tokenAddress is required',
524-
),
525-
periodAmount: toHexOrThrow(
526-
periodAmount,
527-
'Invalid parameters: periodAmount is required',
528-
),
513+
tokenAddress: toHexOrThrow(tokenAddress, 'tokenAddress'),
514+
periodAmount: toHexOrThrow(periodAmount, 'periodAmount'),
529515
periodDuration: Number(periodDuration),
530516
...optionalFields,
531517
},

0 commit comments

Comments
 (0)