[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.
*/
export class TaggedError<Tag extends string = string>
export abstract class TaggedError<Tag extends string = string>
extends Error
implements TaggedVariant<Tag>
{
readonly [VariantTag]: Tag;
constructor(tag: Tag) {
super();
constructor(tag: Tag, message?: string) {
super(message);
this[VariantTag] = tag;
this.name = tag;
}
}

View File

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