# nostics

[nostics](https://www.npmjs.com/package/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

`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`:

```ts [diagnostics.ts]
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();
```

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

| `method` | Severity |
| --- | --- |
| `error` | `error` |
| `warn` or none | `warn` |
| `log` | `info` |

A coding agent gets, with exit code `1`:

```text
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
```

A person at a terminal gets:

```text
✖ 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
```

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:

```json
{
  "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"
  ]
}
```

## fromNostics

`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.

```ts
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")));
```

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

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

The mapping, from [`src/nostics.ts`](https://github.com/loadsheet/readerly/blob/main/src/nostics.ts):

| nostics | `DiagnosticRecord` |
| --- | --- |
| `code`, else `name` | `code` |
| `message`, else `why` | `message` |
| `why`, when it differs from `message` | `why` |
| `fix`, `docs`, `data` | the same fields |
| `sources` | `sources`, 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`](https://github.com/loadsheet/readerly/blob/main/test/nostics.test.ts) runs these round trips against nostics itself.

<!-- vitehub-docs:section-links -->
## In this section

- [Overview](https://readerly.loadsheet.dev/raw/integrations.md)
- [GitHub Actions](https://readerly.loadsheet.dev/raw/integrations/github-actions.md)
- [Argument parsers](https://readerly.loadsheet.dev/raw/integrations/argument-parsers.md)
- [Testing your CLI](https://readerly.loadsheet.dev/raw/integrations/testing.md)
- [Progress](https://readerly.loadsheet.dev/raw/integrations/progress.md)
- [Markdown answers](https://readerly.loadsheet.dev/raw/integrations/markdown-answers.md)
- [nostics](https://readerly.loadsheet.dev/raw/integrations/nostics.md) (this page)

All pages: [llms.txt](https://readerly.loadsheet.dev/llms.txt)
