GitHub
Skip to content

nostics

Send nostics diagnostics to createOut with nosticsReporter, or convert one with fromNostics. The bridge matches nostics by shape and doesn't depend on it.

nostics defines diagnostic codes with a why, a fix and a docs link, and reports them through reporters. readerly's bridge turns each reported diagnostic into a DiagnosticRecord, so createOut prints it for every reader. It lives in readerly/nostics and matches nostics by shape: readerly has no dependency on nostics.

nosticsReporter(sink) returns a nostics reporter that hands each diagnostic to sink.diagnostic(). An Out from createOut is a sink. Add the reporter to defineDiagnostics:

import { defineDiagnostics } from "nostics";
import { createOut } from "readerly";
import { nosticsReporter } from "readerly/nostics";

const out = createOut({ tool: "acme", command: "build", argv: process.argv.slice(2) });

const diagnostics = defineDiagnostics({
  docsBase: "https://acme.dev/errors",
  codes: {
    ACME_E001: {
      why: (p: { name: string }) => `module "${p.name}" failed to load`,
      fix: (p: { name: string }) => `run "npm install ${p.name}"`,
    },
    ACME_W001: { why: "the legacy option is deprecated" },
  },
  reporters: [nosticsReporter(out)],
});

diagnostics.ACME_E001({ name: "left-pad", sources: ["src/app.ts:12:3"] }, { method: "error" });
diagnostics.ACME_W001();
process.exitCode = out.end();
diagnostics.ts

The per-call method sets the severity, as nostics' console reporter routes it:

methodSeverity
errorerror
warn or nonewarn
loginfo

A coding agent gets, with exit code 1:

acme build: failed. 1 error, 1 warning.
error ACME_E001 src/app.ts:12:3 module "left-pad" failed to load. fix: run "npm install left-pad"
warn ACME_W001 the legacy option is deprecated
text

A person at a terminal gets:

✖ 1 error, 1 warning

error ACME_E001  src/app.ts:12:3  module "left-pad" failed to load
╰▶ fix: run "npm install left-pad"
warn  ACME_W001  the legacy option is deprecated
text

The docs links from docsBase are in every record. text shows them as a see: branch with --full, agent as docs: with --full, and json always:

{
  "severity": "error",
  "code": "ACME_E001",
  "message": "module \"left-pad\" failed to load",
  "fix": "run \"npm install left-pad\"",
  "docs": "https://acme.dev/errors/acme_e001",
  "file": "src/app.ts",
  "line": 12,
  "column": 3,
  "sources": [
    "src/app.ts:12:3"
  ]
}
json

fromNostics(d, severity = "warn") converts one diagnostic: a nostics Diagnostic, its toJSON(), or any object of that shape. nostics has no level, so you pass the severity, and it defaults to warn like the console reporter.

import { Diagnostic } from "nostics";
import { fromNostics } from "readerly/nostics";

const d = new Diagnostic({ code: "ACME_E002", why: "config not found", sources: ["acme.config.ts:4"] });
console.log(JSON.stringify(fromNostics(d, "error")));
ts

It prints the record, ready for out.diagnostic():

{"severity":"error","code":"ACME_E002","message":"config not found","file":"acme.config.ts","line":4,"sources":["acme.config.ts:4"]}
json

The mapping, from src/nostics.ts:

nosticsDiagnosticRecord
code, else namecode
message, else whymessage
why, when it differs from messagewhy
fix, docs, datathe same fields
sourcessources, and the first one as file, line and column

A source in file:line:column or file:line form gives the location. Any other string becomes file alone, so acme.config.ts gives file: "acme.config.ts". Windows paths such as C:/a.ts:4:2 keep their drive letter.

test/nostics.test.ts runs these round trips against nostics itself.