Fault
Fault is an Error with a tag, developer details, metadata, causes, HTTP status mapping, and an optional capture hook. Every with* builder returns a new Fault without changing the original.
new Fault
new Fault(initial: Error | string | Fault): Fault
A new fault keeps the input message and forwards only the input error's existing cause; use .withCause(err) to attach the original error itself.
Fault.from
Fault.from(initial: Error | string): Fault
Use Fault.from when converting an error or message without calling the constructor directly.
withTag
withTag(tag: FaultTag): this
withTag returns a new fault with the replacement classification.
withDetails
withDetails(details: string): this
withDetails stores developer-facing context without changing the user-facing message.
withDescription
withDescription(details: string, message?: string): this
withDescription stores developer details and replaces the message only when the optional message is provided.
withMetadata
withMetadata(key: string, value: unknown): this / withMetadata(data: Record<string, unknown>): this
Both overloads return a new fault with merged metadata.
withContext
withContext(data: Record<string, unknown>): this
withContext is the object-form metadata merger used by withMetadata.
withCause
withCause(cause: unknown): this
withCause returns a new fault with the thrown value as its standard Error.cause.
tag
get tag(): FaultTag | undefined
tag returns the current classification; every newly constructed fault starts as UNKNOWN_ERROR.
details
get details(): string | undefined
details returns the developer-facing description when one has been set.
location
get location(): string | undefined
location is the stack location captured when the fault was constructed, when available.
metadata
get metadata(): Record<string, unknown>
metadata returns a shallow copy so changing the returned object does not change the fault.
statusCode
get statusCode(): number
statusCode follows this mapping and defaults unlisted or custom tags to 500.
toJSON
toJSON(): Record<string, unknown>
toJSON emits structured fields for logging, serializes Error causes, and recursively serializes a cause that is another Fault; redact sensitive details, metadata, and non-Error causes before logging.
getCauseChain
getCauseChain(): Array<Error | unknown>
getCauseChain returns this fault followed by each Error cause and any final truthy non-Error cause.
capture / Fault.onCapture
capture(): this / Fault.onCapture: ((fault: Fault) => void) | undefined
capture invokes the current hook when configured and always returns the same fault unchanged.
ServiceError
ServiceError(tag: FaultTag, message: string, description?: string): Fault
ServiceError creates a tagged fault whose details are the description or, when omitted, the message.
FaultTag
FaultTag includes the predefined groups from the source and accepts custom string tags.
Use a predefined tag when it fits; string & {} keeps autocomplete while allowing domain-specific tags.