From 1c4caa3f4207321cb84f0606ea2d08b53c77277c Mon Sep 17 00:00:00 2001 From: Iwo Plaza Date: Wed, 21 Feb 2024 19:31:52 +0100 Subject: [PATCH] Updated what errors are thrown. --- src/error.ts | 23 ++++++++++++++++------- src/index.ts | 1 + src/structure/array.ts | 4 ++-- src/structure/chars.ts | 4 ++-- src/structure/keyed.ts | 14 +++++++------- src/structure/tuple.ts | 4 ++-- 6 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/error.ts b/src/error.ts index 1436230..5f2b3d4 100644 --- a/src/error.ts +++ b/src/error.ts @@ -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); - } -} \ No newline at end of file + // 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); + } +} diff --git a/src/index.ts b/src/index.ts index ec9b254..dbed47e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ export * from './structure'; export * from './describe'; export * from './io'; +export * from './error'; export type { Parsed, ParseUnwrapped } from './utilityTypes'; diff --git a/src/structure/array.ts b/src/structure/array.ts index 90626df..bf16ed5 100644 --- a/src/structure/array.ts +++ b/src/structure/array.ts @@ -1,4 +1,4 @@ -import { TypedBinaryError } from '../error'; +import { ValidationError } from '../error'; import { Measurer, type IMeasurer, @@ -30,7 +30,7 @@ export class ArraySchema extends Schema< write(output: ISerialOutput, values: ParseUnwrapped[]): void { if (values.length !== this.length) { - throw new TypedBinaryError( + throw new ValidationError( `Expected array of length ${this.length}, got ${values.length}`, ); } diff --git a/src/structure/chars.ts b/src/structure/chars.ts index 93a8b62..c240dea 100644 --- a/src/structure/chars.ts +++ b/src/structure/chars.ts @@ -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 { @@ -14,7 +14,7 @@ export class CharsSchema extends Schema { 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}`, ); } diff --git a/src/structure/keyed.ts b/src/structure/keyed.ts index 9364d67..ef9a102 100644 --- a/src/structure/keyed.ts +++ b/src/structure/keyed.ts @@ -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 { @@ -21,31 +21,31 @@ class RefSchema implements ISchema> { } resolve(): void { - throw new TypedBinaryError( + throw new UnresolvedReferenceError( `Tried to resolve a reference directly. Do it through a RefResolver instead.`, ); } read(): Parsed> { - 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.`, ); } @@ -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.`, ); } diff --git a/src/structure/tuple.ts b/src/structure/tuple.ts index 21ef0c2..e1a581e 100644 --- a/src/structure/tuple.ts +++ b/src/structure/tuple.ts @@ -1,4 +1,4 @@ -import { TypedBinaryError } from '../error'; +import { ValidationError } from '../error'; import { Measurer, type IMeasurer, @@ -40,7 +40,7 @@ export class TupleSchema< write(output: ISerialOutput, values: Parsed>): void { if (values.length !== this.schemas.length) { - throw new TypedBinaryError( + throw new ValidationError( `Expected tuple of length ${this.schemas.length}, got ${values.length}`, ); }