Skip to content

Commit 28b5198

Browse files
committed
refactor: use throw new pattern where possible and remove extra error functions
1 parent a94d9c2 commit 28b5198

File tree

18 files changed

+98
-110
lines changed

18 files changed

+98
-110
lines changed

src/collections/custom-key-map.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Account } from '@algorandfoundation/algorand-typescript'
2-
import { internalError } from '../errors'
2+
import { InternalError } from '../errors'
33
import type { StubBytesCompat, StubUint64Compat } from '../impl/primitives'
44
import type { DeliberateAny } from '../typescript-helpers'
55
import { asBytesCls, asUint64Cls } from '../util'
@@ -30,7 +30,7 @@ export abstract class CustomKeyMap<TKey, TValue> implements Map<TKey, TValue> {
3030
getOrFail(key: TKey): TValue {
3131
const value = this.get(key)
3232
if (value === undefined) {
33-
throw internalError('Key not found')
33+
throw new InternalError('Key not found')
3434
}
3535
return value
3636
}

src/context-helpers/internal-context.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Account } from '@algorandfoundation/algorand-typescript'
2-
import { internalError } from '../errors'
2+
import { InternalError } from '../errors'
33
import { BaseContract } from '../impl/base-contract'
44
import type { StubUint64Compat } from '../impl/primitives'
55
import { Uint64Cls } from '../impl/primitives'
@@ -50,7 +50,7 @@ class InternalContext {
5050
getAccountData(account: Account): AccountData {
5151
const data = this.ledger.accountDataMap.get(account)
5252
if (!data) {
53-
throw internalError('Unknown account, check correct testing context is active')
53+
throw new InternalError('Unknown account, check correct testing context is active')
5454
}
5555
return data
5656
}
@@ -59,7 +59,7 @@ class InternalContext {
5959
const key = Uint64Cls.fromCompat(id)
6060
const data = this.ledger.assetDataMap.get(key.asBigInt())
6161
if (!data) {
62-
throw internalError('Unknown asset, check correct testing context is active')
62+
throw new InternalError('Unknown asset, check correct testing context is active')
6363
}
6464
return data
6565
}
@@ -68,15 +68,15 @@ class InternalContext {
6868
const uint64Id = id instanceof BaseContract ? this.ledger.getApplicationForContract(id).id : Uint64Cls.fromCompat(id)
6969
const data = this.ledger.applicationDataMap.get(uint64Id)
7070
if (!data) {
71-
throw internalError('Unknown application, check correct testing context is active')
71+
throw new InternalError('Unknown application, check correct testing context is active')
7272
}
7373
return data
7474
}
7575

7676
getVoterData(account: Account): VoterData {
7777
const data = this.ledger.voterDataMap.get(account)
7878
if (!data) {
79-
throw internalError('Unknown voter, check correct testing context is active')
79+
throw new InternalError('Unknown voter, check correct testing context is active')
8080
}
8181
return data
8282
}

src/encoders.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { biguint, bytes, TransactionType, uint64 } from '@algorandfoundatio
22
import type { OnCompleteAction } from '@algorandfoundation/algorand-typescript/arc4'
33
import { ARC4Encoded } from '@algorandfoundation/algorand-typescript/arc4'
44
import { uint8ArrayToBigInt } from './encoding-util'
5-
import { internalError } from './errors'
5+
import { InternalError } from './errors'
66
import { BytesBackedCls, Uint64BackedCls } from './impl/base'
77
import { arc4Encoders, encodeArc4Impl, getArc4Encoder } from './impl/encoded-types'
88
import { BigUint, Uint64, type StubBytesCompat } from './impl/primitives'
@@ -88,5 +88,5 @@ export const toBytes = (val: unknown): bytes => {
8888
if (Array.isArray(val) || typeof val === 'object') {
8989
return encodeArc4Impl('', val)
9090
}
91-
internalError(`Invalid type for bytes: ${nameOfType(val)}`)
91+
throw new InternalError(`Invalid type for bytes: ${nameOfType(val)}`)
9292
}

src/errors.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ export class InternalError extends Error {
3434
}
3535
}
3636

37-
export function internalError(message: string): never {
38-
throw new InternalError(message)
39-
}
40-
4137
/**
4238
* Raised when unsupported user code is encountered
4339
*/
@@ -47,20 +43,12 @@ export class CodeError extends Error {
4743
}
4844
}
4945

50-
export function codeError(message: string): never {
51-
throw new CodeError(message)
52-
}
53-
5446
export class NotImplementedError extends Error {
5547
constructor(feature: string) {
5648
super(`${feature} is not available in test context. Mock using your preferred testing framework.`)
5749
}
5850
}
5951

60-
export function notImplementedError(feature: string): never {
61-
throw new NotImplementedError(feature)
62-
}
63-
6452
export function testInvariant(condition: unknown, message: string): asserts condition {
6553
if (!condition) {
6654
throw new InternalError(message)

src/impl/crypto.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import js_sha512 from 'js-sha512'
77
import nacl from 'tweetnacl'
88
import { LOGIC_DATA_PREFIX, PROGRAM_TAG } from '../constants'
99
import { lazyContext } from '../context-helpers/internal-context'
10-
import { internalError, notImplementedError } from '../errors'
10+
import { InternalError, NotImplementedError } from '../errors'
1111
import { asBytes, asBytesCls, asUint8Array, conactUint8Arrays } from '../util'
1212
import type { StubBytesCompat, StubUint64Compat } from './primitives'
1313
import { Bytes, BytesCls, Uint64Cls } from './primitives'
@@ -92,7 +92,7 @@ export const ecdsaPkRecover = (
9292
d: StubBytesCompat,
9393
): readonly [bytes, bytes] => {
9494
if (v !== Ecdsa.Secp256k1) {
95-
internalError(`Unsupported ECDSA curve: ${v}`)
95+
throw new InternalError(`Unsupported ECDSA curve: ${v}`)
9696
}
9797
const dataBytes = BytesCls.fromCompat(a)
9898
const rBytes = BytesCls.fromCompat(c)
@@ -124,17 +124,17 @@ export const ecdsaPkDecompress = (v: Ecdsa, a: StubBytesCompat): readonly [bytes
124124
}
125125

126126
export const vrfVerify = (_s: VrfVerify, _a: StubBytesCompat, _b: StubBytesCompat, _c: StubBytesCompat): readonly [bytes, boolean] => {
127-
notImplementedError('vrfVerify')
127+
throw new NotImplementedError('vrfVerify')
128128
}
129129

130130
export const EllipticCurve = new Proxy({} as typeof op.EllipticCurve, {
131131
get: (_target, prop) => {
132-
notImplementedError(`EllipticCurve.${prop.toString()}`)
132+
throw new NotImplementedError(`EllipticCurve.${prop.toString()}`)
133133
},
134134
})
135135

136136
export const mimc = (_c: MimcConfigurations, _a: StubBytesCompat): bytes => {
137-
notImplementedError('mimc')
137+
throw new NotImplementedError('mimc')
138138
}
139139

140140
const curveMap = {

src/impl/emit.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { codeError } from '../errors'
1+
import { CodeError } from '../errors'
22
import type { DeliberateAny } from '../typescript-helpers'
33
import { sha512_256 } from './crypto'
44
import { getArc4Encoded, getArc4TypeName } from './encoded-types'
@@ -14,7 +14,7 @@ export function emitImpl<T>(typeInfoString: string, event: T | string, ...eventP
1414
if (eventName.indexOf('(') === -1) {
1515
eventName += argTypes
1616
} else if (event.indexOf(argTypes) === -1) {
17-
throw codeError(`Event signature ${event} does not match arg types ${argTypes}`)
17+
throw new CodeError(`Event signature ${event} does not match arg types ${argTypes}`)
1818
}
1919
} else {
2020
eventData = getArc4Encoded(event)

src/impl/encoded-types.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {
3434
import { lazyContext } from '../context-helpers/internal-context'
3535
import type { fromBytes, TypeInfo } from '../encoders'
3636
import { uint8ArrayToNumber } from '../encoding-util'
37-
import { avmError, avmInvariant, codeError, CodeError } from '../errors'
37+
import { AvmError, avmInvariant, CodeError } from '../errors'
3838
import type { DeliberateAny } from '../typescript-helpers'
3939
import { asBigInt, asBigUint, asBigUintCls, asBytesCls, asUint64, asUint8Array, conactUint8Arrays } from '../util'
4040
import type { StubBytesCompat } from './primitives'
@@ -301,7 +301,7 @@ const arrayProxyHandler = <TItem>() => ({
301301
const idx = prop ? parseInt(prop.toString(), 10) : NaN
302302
if (!isNaN(idx)) {
303303
if (idx < target.items.length) return target.items[idx]
304-
avmError('Index out of bounds')
304+
throw new AvmError('Index out of bounds')
305305
} else if (prop === Symbol.iterator) {
306306
return target.items[Symbol.iterator].bind(target.items)
307307
} else if (prop === 'entries') {
@@ -320,7 +320,7 @@ const arrayProxyHandler = <TItem>() => ({
320320
target.setItem(idx, value)
321321
return true
322322
}
323-
avmError('Index out of bounds')
323+
throw new AvmError('Index out of bounds')
324324
}
325325

326326
return Reflect.set(target, prop, value)
@@ -575,7 +575,7 @@ export class DynamicArrayImpl<TItem extends ARC4Encoded> extends DynamicArray<TI
575575
pop(): TItem {
576576
const items = this.items
577577
const popped = items.pop()
578-
if (popped === undefined) avmError('The array is empty')
578+
if (popped === undefined) throw new AvmError('The array is empty')
579579
return popped
580580
}
581581

@@ -1255,5 +1255,5 @@ export const getArc4Encoded = (value: DeliberateAny): ARC4Encoded => {
12551255
return new StructImpl(typeInfo, Object.fromEntries(Object.keys(value).map((x, i) => [x, result[i]])))
12561256
}
12571257

1258-
throw codeError(`Unsupported type for encoding: ${typeof value}`)
1258+
throw new CodeError(`Unsupported type for encoding: ${typeof value}`)
12591259
}

src/impl/log.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { bytes, BytesBacked, StringCompat } from '@algorandfoundation/algorand-typescript'
22
import { ARC4Encoded } from '@algorandfoundation/algorand-typescript/arc4'
33
import { lazyContext } from '../context-helpers/internal-context'
4-
import { internalError } from '../errors'
4+
import { InternalError } from '../errors'
55
import { nameOfType } from '../util'
66
import type { StubBigUintCompat, StubBytesCompat, StubUint64Compat } from './primitives'
77
import { AlgoTsPrimitiveCls, BigUintCls, BytesCls, Uint64Cls } from './primitives'
@@ -18,7 +18,7 @@ const toBytes = (val: unknown): bytes => {
1818
case 'number':
1919
return Uint64Cls.fromCompat(val).toBytes().asAlgoTs()
2020
default:
21-
internalError(`Unsupported arg type ${nameOfType(val)}`)
21+
throw new InternalError(`Unsupported arg type ${nameOfType(val)}`)
2222
}
2323
}
2424

src/impl/primitives.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
uint8ArrayToUtf8,
99
utf8ToUint8Array,
1010
} from '../encoding-util'
11-
import { avmError, AvmError, avmInvariant, CodeError, internalError } from '../errors'
11+
import { avmError, AvmError, avmInvariant, CodeError, InternalError } from '../errors'
1212
import { nameOfType } from '../typescript-helpers'
1313
import { base32ToUint8Array } from './base-32'
1414

@@ -211,7 +211,7 @@ export const getNumber = (v: StubUint64Compat): number => {
211211
return Number(v)
212212
}
213213
if (v instanceof Uint64Cls) return v.asNumber()
214-
internalError(`Cannot convert ${v} to number`)
214+
throw new InternalError(`Cannot convert ${v} to number`)
215215
}
216216

217217
export const getUint8Array = (v: StubBytesCompat): Uint8Array => {
@@ -301,7 +301,7 @@ export class Uint64Cls extends AlgoTsPrimitiveCls {
301301
if (typeof v == 'number') return new Uint64Cls(BigInt(v))
302302
if (typeof v == 'bigint') return new Uint64Cls(v)
303303
if (v instanceof Uint64Cls) return v
304-
internalError(`Cannot convert ${v} to uint64`)
304+
throw new InternalError(`Cannot convert ${v} to uint64`)
305305
}
306306

307307
valueOf(): bigint {
@@ -374,7 +374,7 @@ export class BigUintCls extends AlgoTsPrimitiveCls {
374374
if (v instanceof Uint64Cls) return new BigUintCls(v.valueOf())
375375
if (v instanceof BytesCls) return v.toBigUint()
376376
if (v instanceof BigUintCls) return v
377-
internalError(`Cannot convert ${nameOfType(v)} to BigUint`)
377+
throw new InternalError(`Cannot convert ${nameOfType(v)} to BigUint`)
378378
}
379379
}
380380

@@ -519,7 +519,7 @@ export class BytesCls extends AlgoTsPrimitiveCls {
519519
if (typeof v === 'string') return new BytesCls(utf8ToUint8Array(v))
520520
if (v instanceof BytesCls) return v
521521
if (v instanceof Uint8Array) return new BytesCls(v)
522-
internalError(`Cannot convert ${nameOfType(v)} to bytes`)
522+
throw new InternalError(`Cannot convert ${nameOfType(v)} to bytes`)
523523
}
524524

525525
static fromInterpolation(template: TemplateStringsArray, replacements: StubBytesCompat[]) {

0 commit comments

Comments
 (0)