Skip to content

Commit

Permalink
refactor: wrap slice offset out-of-bounds error into a BaseError
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Apr 30, 2023
1 parent 609e843 commit 482aaa1
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/khaki-deers-bake.md
@@ -0,0 +1,5 @@
---
"viem": patch
---

Wrapped slice offset out-of-bounds error in a `BaseError`.
9 changes: 9 additions & 0 deletions src/errors/data.ts
@@ -1,5 +1,14 @@
import { BaseError } from './base.js'

export class SliceOffsetOutOfBoundsError extends BaseError {
override name = 'SliceOffsetOutOfBoundsError'
constructor({ offset, size }: { offset: number; size: number }) {
super(
`Slice starting at offset "${offset}" is out-of-bounds (size: ${size}).`,
)
}
}

export class SizeExceedsPaddingSizeError extends BaseError {
override name = 'SizeExceedsPaddingSizeError'
constructor({
Expand Down
5 changes: 4 additions & 1 deletion src/errors/index.ts
Expand Up @@ -55,7 +55,10 @@ export {
RawContractError,
} from './contract.js'

export { SizeExceedsPaddingSizeError } from './data.js'
export {
SizeExceedsPaddingSizeError,
SliceOffsetOutOfBoundsError,
} from './data.js'

export {
DataLengthTooLongError,
Expand Down
12 changes: 10 additions & 2 deletions src/utils/data/slice.test.ts
Expand Up @@ -31,7 +31,11 @@ test('hex', () => {
expect(sliceHex('0x0123456789', -10)).toMatchInlineSnapshot('"0x0123456789"')

expect(() => sliceHex('0x0123456789', 5)).toThrowErrorMatchingInlineSnapshot(
'"Slice starting at offset \\"5\\" is out-of-bounds (size: 5)."',
`
"Slice starting at offset \\"5\\" is out-of-bounds (size: 5).
Version: viem@1.0.2"
`,
)
})

Expand Down Expand Up @@ -198,6 +202,10 @@ test('bytes', () => {
expect(() =>
sliceBytes(new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), 10),
).toThrowErrorMatchingInlineSnapshot(
'"Slice starting at offset \\"10\\" is out-of-bounds (size: 10)."',
`
"Slice starting at offset \\"10\\" is out-of-bounds (size: 10).
Version: viem@1.0.2"
`,
)
})
7 changes: 2 additions & 5 deletions src/utils/data/slice.ts
@@ -1,3 +1,4 @@
import { SliceOffsetOutOfBoundsError } from '../../errors/data.js'
import type { ByteArray, Hex } from '../../types/index.js'
import { isHex } from './isHex.js'
import { size } from './size.js'
Expand Down Expand Up @@ -25,11 +26,7 @@ export function slice<TValue extends ByteArray | Hex>(

function assertStartOffset(value: Hex | ByteArray, start?: number) {
if (typeof start === 'number' && start > 0 && start > size(value) - 1)
throw new Error(
`Slice starting at offset "${start}" is out-of-bounds (size: ${size(
value,
)}).`,
)
throw new SliceOffsetOutOfBoundsError({ offset: start, size: size(value) })
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/utils/errors/getContractError.ts
@@ -1,5 +1,5 @@
import type { Abi } from 'abitype'
import type { BaseError } from '../../errors/index.js'
import { BaseError } from '../../errors/index.js'
import {
AbiDecodingZeroDataError,
ContractFunctionExecutionError,
Expand Down Expand Up @@ -34,7 +34,9 @@ export function getContractError(
const { code, data, message, shortMessage } = (
err instanceof RawContractError
? err
: err.walk((err) => 'data' in (err as Error))
: err instanceof BaseError
? err.walk((err) => 'data' in (err as Error))
: {}
) as RawContractError

let cause = err
Expand Down

1 comment on commit 482aaa1

@vercel
Copy link

@vercel vercel bot commented on 482aaa1 Apr 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.