Skip to content

knotkit/errors

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

import {
function isKnotkitError(value: unknown): value is KnotkitErrorShape

Whether value carries the moduleName, _tag, code, and message fields every Knotkit error exposes. Structural, so it recognises an error from another realm or a duplicate bundle copy.

isKnotkitError
,
function isParseError(value: unknown): value is ParseErrorShape

Whether value is a Knotkit error in the ParseError category.

isParseError
} from 'knotkit/errors'
import {
function deserializeSnapshotSet<T>(value: unknown, options: DeserializeSnapshotSetOptions<T>): SnapshotSet<T>

Restore a binary snapshot set after validating all counts, histories, snapshot identities, codec results, and duplicate values in staging data.

@throws ― {SnapshotSetInvalidInputError} With MISSING_ELEMENT_CODEC when options.codec is absent: supplying the codec is a caller contract, not a property of the bytes.

@throws ― {SnapshotSetParseError} For malformed bytes, unsupported versions, invalid histories, duplicate values, or decode-budget violations.

deserializeSnapshotSet
} from 'knotkit/snapshot-set/wire'
import {
const stringCodec: ElementCodec<string>

A length-prefixed, strict UTF-8 codec for strings.

stringCodec
} from 'knotkit/wire'
const
const payload: Uint8Array<ArrayBuffer>
payload
= new
var Uint8Array: Uint8ArrayConstructor
new () => Uint8Array<ArrayBuffer> (+6 overloads)
Uint8Array
() // Empty payload triggers a parse error.
try {
deserializeSnapshotSet<string>(value: unknown, options: DeserializeSnapshotSetOptions<string>): SnapshotSet<string>

Restore a binary snapshot set after validating all counts, histories, snapshot identities, codec results, and duplicate values in staging data.

@throws ― {SnapshotSetInvalidInputError} With MISSING_ELEMENT_CODEC when options.codec is absent: supplying the codec is a caller contract, not a property of the bytes.

@throws ― {SnapshotSetParseError} For malformed bytes, unsupported versions, invalid histories, duplicate values, or decode-budget violations.

deserializeSnapshotSet
(
const payload: Uint8Array<ArrayBuffer>
payload
, {
DeserializeSnapshotSetOptions<string>.codec: ElementCodec<string>

Codec matching the one used to serialize values.

codec
:
const stringCodec: ElementCodec<string>

A length-prefixed, strict UTF-8 codec for strings.

stringCodec
})
} catch (
var error: unknown
error
) {
if (
function isParseError(value: unknown): value is ParseErrorShape

Whether value is a Knotkit error in the ParseError category.

isParseError
(
var error: unknown
error
)) {
var console: Console
console
.
Console.error(...data: any[]): void (+1 overload)

Log to stderr in your terminal

Appears in red

@param ― data something to display

error
('rejected payload:',
var error: ParseErrorShape
error
.
KnotkitErrorShape.moduleName: ModuleName
moduleName
,
var error: ParseErrorShape
error
.
KnotkitErrorShape.code: string
code
)
} else if (
function isKnotkitError(value: unknown): value is KnotkitErrorShape

Whether value carries the moduleName, _tag, code, and message fields every Knotkit error exposes. Structural, so it recognises an error from another realm or a duplicate bundle copy.

isKnotkitError
(
var error: unknown
error
)) {
var console: Console
console
.
Console.error(...data: any[]): void (+1 overload)

Log to stderr in your terminal

Appears in red

@param ― data something to display

error
(
var error: KnotkitErrorShape
error
.
KnotkitErrorShape.moduleName: ModuleName
moduleName
,
var error: KnotkitErrorShape
error
.
KnotkitErrorShape._tag: string
_tag
,
var error: KnotkitErrorShape
error
.
KnotkitErrorShape.code: string
code
,
var error: KnotkitErrorShape
error
.
KnotkitErrorShape.message: string
message
)
} else {
throw
var error: unknown
error
}
}
errors.ts
  • 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
ExportReturnsDoes
safeStringstringFormat an unknown value for a message without letting a hostile toString or Symbol.toPrimitive replace the caller's error.
If you need to…Use instead
throw or catch errors in an application that depends on no Knotkit packagethe platform Error class or a project-local subclass with its own codes
throw a concrete error from your own Knotkit packageextend 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 boundaryan 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 packagea schema validator such as Zod, or a project-local error class

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

Prompt · knotkit/errors
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/