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
readonly
[FAILURE_BRAND]: true
Cross-realm brand read by isFailure(). Non-enumerable so it never reaches the wire.
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 '{}'