Skip to content

Commit 1c4caa3

Browse files
committed
Updated what errors are thrown.
1 parent 5c6d1bc commit 1c4caa3

File tree

6 files changed

+30
-20
lines changed

6 files changed

+30
-20
lines changed

src/error.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1-
export class TypedBinaryError extends Error {
2-
constructor(msg: string) {
3-
super(msg);
1+
export class UnresolvedReferenceError extends Error {
2+
constructor(msg: string) {
3+
super(msg);
44

5-
// Set the prototype explicitly.
6-
Object.setPrototypeOf(this, TypedBinaryError.prototype);
7-
}
8-
}
5+
// Set the prototype explicitly.
6+
Object.setPrototypeOf(this, UnresolvedReferenceError.prototype);
7+
}
8+
}
9+
10+
export class ValidationError extends Error {
11+
constructor(msg: string) {
12+
super(msg);
13+
14+
// Set the prototype explicitly.
15+
Object.setPrototypeOf(this, ValidationError.prototype);
16+
}
17+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from './structure';
22
export * from './describe';
33
export * from './io';
4+
export * from './error';
45

56
export type { Parsed, ParseUnwrapped } from './utilityTypes';

src/structure/array.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TypedBinaryError } from '../error';
1+
import { ValidationError } from '../error';
22
import {
33
Measurer,
44
type IMeasurer,
@@ -30,7 +30,7 @@ export class ArraySchema<TElement extends AnySchema> extends Schema<
3030

3131
write(output: ISerialOutput, values: ParseUnwrapped<TElement>[]): void {
3232
if (values.length !== this.length) {
33-
throw new TypedBinaryError(
33+
throw new ValidationError(
3434
`Expected array of length ${this.length}, got ${values.length}`,
3535
);
3636
}

src/structure/chars.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
type ISerialInput,
55
type ISerialOutput,
66
} from '../io';
7-
import { TypedBinaryError } from '../error';
7+
import { ValidationError } from '../error';
88
import { Schema } from './types';
99

1010
export class CharsSchema extends Schema<string> {
@@ -14,7 +14,7 @@ export class CharsSchema extends Schema<string> {
1414

1515
write(output: ISerialOutput, value: string): void {
1616
if (value.length !== this.length) {
17-
throw new TypedBinaryError(
17+
throw new ValidationError(
1818
`Expected char-string of length ${this.length}, got ${value.length}`,
1919
);
2020
}

src/structure/keyed.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TypedBinaryError } from '../error';
1+
import { UnresolvedReferenceError } from '../error';
22
import { IMeasurer, ISerialInput, ISerialOutput, Measurer } from '../io';
33
import { ParseUnwrapped, Parsed } from '../utilityTypes';
44
import {
@@ -21,31 +21,31 @@ class RefSchema<TKeyDef extends string> implements ISchema<Ref<TKeyDef>> {
2121
}
2222

2323
resolve(): void {
24-
throw new TypedBinaryError(
24+
throw new UnresolvedReferenceError(
2525
`Tried to resolve a reference directly. Do it through a RefResolver instead.`,
2626
);
2727
}
2828

2929
read(): Parsed<Ref<TKeyDef>> {
30-
throw new TypedBinaryError(
30+
throw new UnresolvedReferenceError(
3131
`Tried to read a reference directly. Resolve it instead.`,
3232
);
3333
}
3434

3535
write(): void {
36-
throw new TypedBinaryError(
36+
throw new UnresolvedReferenceError(
3737
`Tried to write a reference directly. Resolve it instead.`,
3838
);
3939
}
4040

4141
measure(): IMeasurer {
42-
throw new TypedBinaryError(
42+
throw new UnresolvedReferenceError(
4343
`Tried to measure size of a reference directly. Resolve it instead.`,
4444
);
4545
}
4646

4747
seekProperty(): PropertyDescription | null {
48-
throw new TypedBinaryError(
48+
throw new UnresolvedReferenceError(
4949
`Tried to seek property of a reference directly. Resolve it instead.`,
5050
);
5151
}
@@ -70,7 +70,7 @@ class RefResolve implements IRefResolver {
7070
return this.registry[key] as TSchema;
7171
}
7272

73-
throw new TypedBinaryError(
73+
throw new UnresolvedReferenceError(
7474
`Couldn't resolve reference to ${key}. Unknown key.`,
7575
);
7676
}

src/structure/tuple.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TypedBinaryError } from '../error';
1+
import { ValidationError } from '../error';
22
import {
33
Measurer,
44
type IMeasurer,
@@ -40,7 +40,7 @@ export class TupleSchema<
4040

4141
write(output: ISerialOutput, values: Parsed<UnwrapArray<TSequence>>): void {
4242
if (values.length !== this.schemas.length) {
43-
throw new TypedBinaryError(
43+
throw new ValidationError(
4444
`Expected tuple of length ${this.schemas.length}, got ${values.length}`,
4545
);
4646
}

0 commit comments

Comments
 (0)