Custom error types and error factory functions
npm install @thi.ng/errors
!npm downloads

> [!NOTE]
> This is one of 214 standalone projects, maintained as part
> of the @thi.ng/umbrella monorepo
> and anti-framework.
>
> 🚀 Please help me to work full-time on these projects by sponsoring me on
> GitHub. Thank you! ❤️
- About
- Status
- Installation
- Dependencies
- Usage examples
- API
- Environment variables
- Authors
- License
Custom error types and error factory functions.
This package defines the following error types & helper functions to throw them:
- AssertionError
- IllegalArgumentError
- IllegalArityError
- IllegalStateError
- OutOfBoundsError
- UnsupportedOperationError
Custom error types can be easily defined usingdefError().
STABLE - used in production
Search or submit any issues for this package
``bash`
yarn add @thi.ng/errors
ESM import:
`ts`
import * as err from "@thi.ng/errors";
Browser ESM import:
`html`
For Node.js REPL:
`js`
const err = await import("@thi.ng/errors");
Package sizes (brotli'd, pre-treeshake): ESM: 849 bytes
None
One project in this repo's
/examples
directory is using this package:
| Description | Live demo | Source |
|:-----------------------------------------------------------------------------|:---------------------------------------------------|:--------------------------------------------------------------------------------|
| GPU-based data reduction using thi.ng/shader-ast & WebGL multi-pass pipeline | Demo | Source |
`ts
import * as err from "@thi.ng/errors";
err.illegalArity(3)
// Error: illegal arity: 3
err.illegalArgs("expected foo");
// Error: illegal argument(s): expected foo
err.illegalState("oops");
// Error: illegal state: oops
err.unsupported("TODO not yet implemented")
// Error: unsupported operation: TODO not yet implemented
// define custom error
const MyError = err.defError(
() => "Eeek... ",
(x) => x + " is not allowed!"
);
try {
throw new MyError(23);
} catch(e) {
console.warn(e.message);
console.log(e instanceof Error);
}
// Eeek... 23 is not allowed!
// true
`
The UMBRELLA_ASSERTS or VITE_UMBRELLA_ASSERTS env variables areassert()
used to control the behavior of the function in production builds: Ifassert()
either is set (to a non-empty string), the function will always be enabled.
Otherwise (by default), will be disabled for production builds,process.env.NODE_ENV === "production"
i.e. if .
If this project contributes to an academic publication, please cite it as:
`bibtex``
@misc{thing-errors,
title = "@thi.ng/errors",
author = "Karsten Schmidt",
note = "https://thi.ng/errors",
year = 2018
}
© 2018 - 2026 Karsten Schmidt // Apache License 2.0