Defjs
Core

Fault

Union of every failure an operation can return, discriminated by code.

@defjs/core · Type

Signature

type Fault<TErr extends AnyStruct | undefined = undefined> = FaultOf<
  ErrorBody<TErr>
>;

Fault<TErr> is FaultOf applied to the decoded body type, so it is the union of DecodeFault, HttpStatusFault<TErr>, and PreflightFault. ErrorBody<TErr> is undefined when TErr is undefined, and Infer<TErr> otherwise. Switch on fault.code.

Every variant is a native Error named DefjsFault, so String(fault) produces DefjsFault: <message>. code and the variant metadata — status, response, data — are enumerable own properties. The native cause chain is non-enumerable.

Default Struct messages say received <kind>, never the received value. A JSON SyntaxError behind RES_DECODE_FAILED gives the message Response body could not be decoded. Structural paths retain record keys, which may be sensitive. Raw StructIssue.received and parser causes remain sensitive; custom errorMap and extension messages are used verbatim and need review before logging.

Call format(), flatten(), or prettify() only after narrowing fault.cause to StructError; those helpers are not copied onto the fault.

Examples

import { StructError, type Fault } from "@defjs/core";

function logFault(fault: Fault): void {
  console.error(String(fault), { code: fault.code });
  if (fault.cause instanceof StructError) {
    console.error(fault.cause.prettify());
  }
}
Edit this page on GitHub
Released under the MIT License. · Copyright © 2026 MunMunMiao