class Failure
extends Error

A structured, discriminable error.

Construct these through define() rather than directly: the factory is what pins code to a literal type and gives you a matching type guard.

const failure = new Failure('FileMissing', 'no such file: a.ts', { path: 'a.ts' });
failure.code; // 'FileMissing' — a literal type, so `switch` narrows
failure.data.path; // 'a.ts' — typed payload, no message parsing
failure instanceof Error; // true — devtools, loggers and `util.inspect` keep working

Constructors

Failure(
code: Code,
message: string,
data: Data,
options?: FailureOptions
)

Prefer define(), which pins code to a literal type and supplies the stack anchor.

Type Parameters

Code extends string = string
Data = unknown

Properties

readonly
[FAILURE_BRAND]: true

Cross-realm brand read by isFailure(). Non-enumerable so it never reaches the wire.

readonly
code: Code

The discriminant. Narrow on this, never on instanceof.

readonly
data: Data

Structured payload supplied by the throw site.

Methods

Serializes to plain JSON so console.log(JSON.stringify(failure)) is not {}.

import * as Failure from './failure.ts';

const Denied = Failure.define('Denied', 'permission denied');
JSON.stringify(Denied()); // JSON.stringify calls this — the full wire form, not '{}'

Usage

import { Failure } from "lib/result/failure.ts";