[fabric/core] Refactor TaggedError and UnexpectedError to accept custom error messages
This commit is contained in:
parent
1886c52ece
commit
7a56c34941
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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)}`;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user