[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.
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user