Skip to main content
Version: v0.0.3

Function: extractStableCode()

extractStableCode(e): string | null

Pull a code off a thrown value only when it is a PHI-free constant token (^[A-Z][A-Z0-9_]*$: a letter-led UPPER_SNAKE code like MALFORMED_JSON), e.g. a wrapped parser's stable fatal code. Anything else (no code, a non-string, a non-token, or a pure-digit value that could be a raw identifier) yields null, so a parser exception that embedded input bytes in a code-shaped field can never reach a diagnostic.

Parameters

e

unknown

A caught value.

Returns

string | null

The stable code token, or null.

Example

import { extractStableCode } from "@cosyte/cli";

extractStableCode({ code: "MALFORMED_JSON" }); // => "MALFORMED_JSON"
extractStableCode(new Error("boom")); // => null