Skip to content

Commit

Permalink
Custom error message instead "can't convert to BigInt"
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Nov 22, 2021
1 parent 37c72be commit 3348eb1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/types/FateBytes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ const toByteArray = (value) => {
return HexStringToByteArray(value)
}

return Int2ByteArray(BigInt(value))
try {
return Int2ByteArray(BigInt(value))
} catch (error) {
if (!/^Cannot convert .* to a BigInt$/.test(error.message)) throw error
}

throw new Error(`Should be one of: Array, ArrayBuffer, hex string, Number, BigInt; got ${value} instead`)
}

class FateBytes extends FateData {
Expand Down
8 changes: 8 additions & 0 deletions tests/Encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ test('Encode hash arguments', t => {
)
})

test('Ensures hash in a proper format', t => {
t.plan(1)
t.throws(
() => encoder.encode(CONTRACT, 'test_hash', [{}]),
{ message: 'Should be one of: Array, ArrayBuffer, hex string, Number, BigInt; got [object Object] instead' }
)
})

test('Encode signature arguments', t => {
t.plan(1)
const encoded = encoder.encode(
Expand Down

0 comments on commit 3348eb1

Please sign in to comment.