Skip to content

Commit

Permalink
Updated what errors are thrown.
Browse files Browse the repository at this point in the history
  • Loading branch information
iwoplaza committed Feb 21, 2024
1 parent 5c6d1bc commit 1c4caa3
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 20 deletions.
23 changes: 16 additions & 7 deletions src/error.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
export class TypedBinaryError extends Error {
constructor(msg: string) {
super(msg);
export class UnresolvedReferenceError extends Error {
constructor(msg: string) {
super(msg);

// Set the prototype explicitly.
Object.setPrototypeOf(this, TypedBinaryError.prototype);
}
}
// Set the prototype explicitly.
Object.setPrototypeOf(this, UnresolvedReferenceError.prototype);
}
}

export class ValidationError extends Error {
constructor(msg: string) {
super(msg);

// Set the prototype explicitly.
Object.setPrototypeOf(this, ValidationError.prototype);
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './structure';
export * from './describe';
export * from './io';
export * from './error';

export type { Parsed, ParseUnwrapped } from './utilityTypes';
4 changes: 2 additions & 2 deletions src/structure/array.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TypedBinaryError } from '../error';
import { ValidationError } from '../error';
import {
Measurer,
type IMeasurer,
Expand Down Expand Up @@ -30,7 +30,7 @@ export class ArraySchema<TElement extends AnySchema> extends Schema<

write(output: ISerialOutput, values: ParseUnwrapped<TElement>[]): void {
if (values.length !== this.length) {
throw new TypedBinaryError(
throw new ValidationError(
`Expected array of length ${this.length}, got ${values.length}`,
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/structure/chars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
type ISerialInput,
type ISerialOutput,
} from '../io';
import { TypedBinaryError } from '../error';
import { ValidationError } from '../error';
import { Schema } from './types';

export class CharsSchema extends Schema<string> {
Expand All @@ -14,7 +14,7 @@ export class CharsSchema extends Schema<string> {

write(output: ISerialOutput, value: string): void {
if (value.length !== this.length) {
throw new TypedBinaryError(
throw new ValidationError(
`Expected char-string of length ${this.length}, got ${value.length}`,
);
}
Expand Down
14 changes: 7 additions & 7 deletions src/structure/keyed.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TypedBinaryError } from '../error';
import { UnresolvedReferenceError } from '../error';
import { IMeasurer, ISerialInput, ISerialOutput, Measurer } from '../io';
import { ParseUnwrapped, Parsed } from '../utilityTypes';
import {
Expand All @@ -21,31 +21,31 @@ class RefSchema<TKeyDef extends string> implements ISchema<Ref<TKeyDef>> {
}

resolve(): void {
throw new TypedBinaryError(
throw new UnresolvedReferenceError(
`Tried to resolve a reference directly. Do it through a RefResolver instead.`,
);
}

read(): Parsed<Ref<TKeyDef>> {
throw new TypedBinaryError(
throw new UnresolvedReferenceError(
`Tried to read a reference directly. Resolve it instead.`,
);
}

write(): void {
throw new TypedBinaryError(
throw new UnresolvedReferenceError(
`Tried to write a reference directly. Resolve it instead.`,
);
}

measure(): IMeasurer {
throw new TypedBinaryError(
throw new UnresolvedReferenceError(
`Tried to measure size of a reference directly. Resolve it instead.`,
);
}

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

throw new TypedBinaryError(
throw new UnresolvedReferenceError(
`Couldn't resolve reference to ${key}. Unknown key.`,
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/structure/tuple.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TypedBinaryError } from '../error';
import { ValidationError } from '../error';
import {
Measurer,
type IMeasurer,
Expand Down Expand Up @@ -40,7 +40,7 @@ export class TupleSchema<

write(output: ISerialOutput, values: Parsed<UnwrapArray<TSequence>>): void {
if (values.length !== this.schemas.length) {
throw new TypedBinaryError(
throw new ValidationError(
`Expected tuple of length ${this.schemas.length}, got ${values.length}`,
);
}
Expand Down

0 comments on commit 1c4caa3

Please sign in to comment.