# knotkit/errors

> Provides the base error classes every Knotkit package throws from, discriminated by package, category, and cause

## Use

```ts
import { isKnotkitError, isParseError } from 'knotkit/errors'
import { deserializeSnapshotSet } from 'knotkit/snapshot-set/wire'
import { stringCodec } from 'knotkit/wire'

const payload = new Uint8Array() // Empty payload triggers a parse error.
try {
  deserializeSnapshotSet(payload, { codec: stringCodec })
} catch (error) {
  if (isParseError(error)) {
    console.error('rejected payload:', error.moduleName, error.code)
  } else if (isKnotkitError(error)) {
    console.error(error.moduleName, error._tag, error.code, error.message)
  } else {
    throw error
  }
}
```

## What it solves

* catch a failure from any Knotkit package without importing each package's own error classes
* read which package failed, which category, and the exact code from a caught error
* recognize a Knotkit error that crossed a realm or came from a duplicate copy of a package, where instanceof reports a false negative
* format an unknown thrown value into a message without a hostile toString replacing the caller's error
* fail a decode on the first invalid element using the throwing package's own error class and code

## API

| Export                                                          | Returns  | Does                                                                                                                       |
| --------------------------------------------------------------- | -------- | -------------------------------------------------------------------------------------------------------------------------- |
| [`safeString`](/reference/knotkit/errors/functions/safestring/) | `string` | Format an unknown value for a message without letting a hostile toString or Symbol.toPrimitive replace the caller's error. |

## When not to use it

| If you need to…                                                                         | Use instead                                                                                  |
| --------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------- |
| throw or catch errors in an application that depends on no Knotkit package              | the platform Error class or a project-local subclass with its own codes                      |
| throw a concrete error from your own Knotkit package                                    | extend InvalidInputError or ParseError with a package-scoped subclass and a closed code list |
| carry an error's package name, category, and code across a process or JSON boundary     | an explicit error envelope marshalled by the caller, because no class here serializes        |
| validate caller input or decode a payload for a structure that is not a Knotkit package | a schema validator such as Zod, or a project-local error class                               |

## Performance

This module has no benchmark suite. The costs above are its performance contract.

## Prompt an agent

Prompt · knotkit/errorsCopy prompt

```
Use knotkit/errors for this task. If Knotkit is missing, follow https://knotkit.jkomyno.dev/getting-started.md#install-from-source. Import from 'knotkit/errors'.

Exports:
- `safeString` → `string`: Format an unknown value for a message without letting a hostile toString or Symbol.toPrimitive replace the caller's error.

Use something else when:
- throw or catch errors in an application that depends on no Knotkit package → the platform Error class or a project-local subclass with its own codes
- throw a concrete error from your own Knotkit package → extend InvalidInputError or ParseError with a package-scoped subclass and a closed code list
- carry an error's package name, category, and code across a process or JSON boundary → an explicit error envelope marshalled by the caller, because no class here serializes
- validate caller input or decode a payload for a structure that is not a Knotkit package → a schema validator such as Zod, or a project-local error class

Full page: https://knotkit.jkomyno.dev/modules/errors.md
Types: node_modules/knotkit/errors/ and https://knotkit.jkomyno.dev/reference/knotkit/errors/
```

## See also

* [API reference](/reference/knotkit/errors/)every type and signature
* [Capability record](https://github.com/jkomyno/knotkit/blob/main/packages/errors/knotkit.capability.json)this page as JSON, also in the installed package
* [Edit this page on GitHub](https://github.com/jkomyno/knotkit/edit/main/packages/errors/knotkit.capability.json)suggest a change to the source record
