Skip to content

Commit

Permalink
Merge branch 'master' into st/feat/add-generate-fake-resources-to-acc…
Browse files Browse the repository at this point in the history
…ount
  • Loading branch information
Torres-ssf authored May 29, 2024
2 parents 2be51f8 + 5a6ca46 commit 32023c4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/big-ducks-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/utils": patch
---

feat: improve `arrayify` error message
20 changes: 18 additions & 2 deletions packages/utils/src/utils/arrayify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,30 @@ describe('arrayify', () => {
test('should throw a FuelError for an invalid hex string', () => {
const invalidHexString = '0xgg0102030405';
expect(() => arrayify(invalidHexString)).toThrowError(
new FuelError(ErrorCode.INVALID_DATA, 'invalid data - ')
new FuelError(
ErrorCode.INVALID_DATA,
'invalid data: 0xgg0102030405\nIf you are attempting to transform a hex value, please make sure it is being passed as a string and wrapped in quotes.'
)
);
});

test('should throw a FuelError for an invalid input', () => {
const invalidInput = 123 as unknown as BytesLike;
expect(() => arrayify(invalidInput, 'invalid input')).toThrowError(
new FuelError(ErrorCode.INVALID_DATA, 'invalid data - invalid input')
new FuelError(
ErrorCode.INVALID_DATA,
'invalid data: invalid input - 123\nIf you are attempting to transform a hex value, please make sure it is being passed as a string and wrapped in quotes.'
)
);
});

test('should throw for integer hex value', () => {
const invalidHexString = 0xd5579c46 as unknown as BytesLike;
expect(() => arrayify(invalidHexString, 'hex')).toThrowError(
new FuelError(
ErrorCode.INVALID_DATA,
'invalid data: hex - 3579288646\nIf you are attempting to transform a hex value, please make sure it is being passed as a string and wrapped in quotes.'
)
);
});
});
4 changes: 3 additions & 1 deletion packages/utils/src/utils/arrayify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ export const arrayify = (value: BytesLike, name?: string, copy: boolean = true):
return result;
}

throw new FuelError(ErrorCode.INVALID_DATA, `invalid data - ${name || ''}`);
const nameMessage = name ? ` ${name} -` : '';
const message = `invalid data:${nameMessage} ${value}\nIf you are attempting to transform a hex value, please make sure it is being passed as a string and wrapped in quotes.`;
throw new FuelError(ErrorCode.INVALID_DATA, message);
};

0 comments on commit 32023c4

Please sign in to comment.