[fabric/core] Refactor TaggedError and UnexpectedError to accept custom error messages

This commit is contained in:
Pablo Baleztena 2024-10-15 14:58:22 -03:00
parent 1886c52ece
commit 7a56c34941
2 changed files with 6 additions and 10 deletions

View File

@ -3,14 +3,15 @@ import { TaggedVariant, VariantTag } from "../variant/index.js";
/** /**
* A TaggedError is a tagged variant with an error message. * A TaggedError is a tagged variant with an error message.
*/ */
export class TaggedError<Tag extends string = string> export abstract class TaggedError<Tag extends string = string>
extends Error extends Error
implements TaggedVariant<Tag> implements TaggedVariant<Tag>
{ {
readonly [VariantTag]: Tag; readonly [VariantTag]: Tag;
constructor(tag: Tag) { constructor(tag: Tag, message?: string) {
super(); super(message);
this[VariantTag] = tag; this[VariantTag] = tag;
this.name = tag;
} }
} }

View File

@ -8,12 +8,7 @@ import { TaggedError } from "./tagged-error.js";
* we must be prepared to handle. * we must be prepared to handle.
*/ */
export class UnexpectedError extends TaggedError<"UnexpectedError"> { export class UnexpectedError extends TaggedError<"UnexpectedError"> {
constructor(readonly context: Record<string, unknown> = {}) { constructor(message?: string) {
super("UnexpectedError"); super("UnexpectedError", message);
this.message = "An unexpected error occurred";
}
toString() {
return `UnexpectedError: ${this.message}\n${JSON.stringify(this.context, null, 2)}`;
} }
} }