https://github.com/openinf/openinf-util-errors
Essential utility errors inspired by Node.js core error codes
https://github.com/openinf/openinf-util-errors
class-library commonjs commonjs-module commonjs-package error-classes error-codes errors javascript javascript-library nodejs nodejs-module npm npm-package typescript typescript-library typescript-module typescript-package
Last synced: 9 months ago
JSON representation
Essential utility errors inspired by Node.js core error codes
- Host: GitHub
- URL: https://github.com/openinf/openinf-util-errors
- Owner: OpenINF
- License: other
- Created: 2021-01-29T08:05:10.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2025-04-01T13:44:26.000Z (9 months ago)
- Last Synced: 2025-04-01T14:38:30.347Z (9 months ago)
- Topics: class-library, commonjs, commonjs-module, commonjs-package, error-classes, error-codes, errors, javascript, javascript-library, nodejs, nodejs-module, npm, npm-package, typescript, typescript-library, typescript-module, typescript-package
- Language: TypeScript
- Homepage: https://github.com/OpenINF/openinf-util-errors#readme
- Size: 249 KB
- Stars: 4
- Watchers: 3
- Forks: 1
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
- Security: SECURITY.md
- Authors: AUTHORS
Awesome Lists containing this project
README
## `@openinf/util-errors`
> Essential utility errors inspired by Node.js core error codes
[!['View on npm'][npm-badge--shields]][npm-badge-url]
[!['License: MIT/Apache-2.0'][license-badge--shields]][license-badge-url]
The high-level goal of `@openinf/util-errors` is to serve as a Node.js package
containing **essential utility error classes** that take the form of those found
and used in Node.js core. We are constantly working to improve this repository,
so please feel free to [contribute](#contributing) if you notice any omissions
or errors.
Thanks!
Supported Node.js Environments
- [ ] v4:Argon (Ar)
- [ ] v6:Boron (B)
- [ ] v8:Carbon (C)
- [ ] v10:Dubnium (Db)
- [ ] v12:Erbium (Er)
- [x] v14:Fermium (Fm)
- [x] v16:Gallium (Ga)
- [x] v18:Hydrogen (H)
[![Code Style: Prettier][prettier-badge]][prettier-url]
[![Commit Style: Conventional Commits][conventional-commits-badge]][conventional-commits-url]
[![Chat on Matrix][matrix-badge--shields]][matrix-url]
---
### Table of Contents
- [Installation](#installation)
- [Usage](#usage)
- [API](#api)
- [Contributing](#contributing)
- [License](#license)
---
Installation
`@openinf/util-errors` runs on
[supported versions of Node.js](#platform--node-js-lts) and is available via
**`npm`**, **`pnpm`**, or **`yarn`**.
**Using the npm CLI**
See the
[official documentation for this command](https://docs.npmjs.com/cli/commands/npm-install)
for more information.
```shell
npm i @openinf/util-errors
```
**Using the pnpm CLI**
See the [official documentation for this command](https://pnpm.io/cli/add)
for more information.
```shell
pnpm add @openinf/util-errors
```
**Using the Yarn 1 CLI (Classic)**
See the
[official documentation for this command](https://classic.yarnpkg.com/en/docs/cli/add)
for more information.
```shell
yarn add @openinf/util-errors
```
### Usage
To get started using the error classes provided by `@openinf/util-errors`, all
that needs to be done is either import/require (depending on the module format)
the default export of the module or destructure the individual named error
classes exported.
```ts
import { hasOwn } from '@openinf/util-object';
import { MissingOptionError } from '@openinf/util-errors';
import infLog from '@openinf/inf-log';
function getLogger(logger, opts) {
if (!hasOwn(opts, 'scope')) {
throw new MissingOptionError('scope');
}
return new logger(opts);
}
const log = getLogger(infLog, infLog.defaultOpts);
log.info('Hello, World!');
```
**Note:** The example above does not demonstrate how to properly handle this error
once thrown and would likely result in an uncaught exception.
**Note:** If you are in an environment where the CommonJS module loader
(`require()`) is available, destructuring the individual error classes works
just as well.
### API
- InvalidArgValueError
-
Thrown in case an invalid or unsupported value was passed for a given argument.
- InvalidArgTypeError
-
Thrown in case an argument of the wrong type was passed for a given argument.
- InvalidPropertyValueError
-
Thrown in case an invalid or unsupported value of an object property.
- InvalidPropertyTypeError
-
Thrown in case an invalid or unsupported value type for an object property.
- InvalidReturnPropertyValueError
-
Thrown in case a function does not provide a valid value for one of
its returned object properties on execution. - InvalidReturnPropertyTypeError
-
Thrown in case a function does not provide an expected value type for
one of its returned object properties on execution. - InvalidReturnValueError
-
Thrown in case a function does not return an expected valid value on
execution. - InvalidReturnTypeError
-
Thrown in case a function does not return an expected value type on
execution, such as when a function is expected to return a promise. - InvalidArgsNumberError
-
Thrown in case the number of arguments passed to a function is invalid.
- MissingOptionError
-
For APIs that accept options objects, some options might be mandatory. This
error is thrown if a required option is missing. - MissingArgsError
-
Thrown in case a required argument of an API was not passed.
This is only used for strict compliance with the API specification (which in
some cases may acceptfunc(undefined)but notfunc()). In most native
Node.js APIs,func(undefined)andfunc()are treated identically, and theERR_INVALID_ARG_TYPEerror code may be used instead. - UnhandledErrorError
-
Thrown in case an unhandled error occurred (for instance, when an 'error'
event is emitted by an EventEmitter without an 'error' handler registered).
#### InvalidArgValueError
Thrown in case an invalid or unsupported value was passed for a given argument.
**Kind**: global class
**See**: https://nodejs.org/api/errors.html#ERR_INVALID_ARG_VALUE
##### new InvalidArgValueError(argName, value, reason)
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| argName | string | | The argument name. |
| value | unknown | | The actual invalid argument value. |
| reason | string | "is invalid" | The reason for invalidity. |
#### InvalidArgTypeError
Thrown in case an argument of the wrong type was passed for a given argument.
**Kind**: global class
**See**: https://nodejs.org/api/errors.html#ERR_INVALID_ARG_TYPE
##### new InvalidArgTypeError(argName, expected, value)
| Param | Type | Description |
| --- | --- | --- |
| argName | string | The name of the argument of invalid type. |
| expected | Array<string> \| string | The argument type(s) expected. |
| value | unknown | The actual argument value of invalid type. |
#### InvalidPropertyValueError
Thrown in case an invalid or unsupported value of an object property.
##### new InvalidPropertyValueError(objName, propName, value, reason)
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| objName | string | | The name of the object in question. |
| propName | string | | The property name assigned invalid value. |
| value | unknown | | The actual invalid property value assigned. |
| reason | string | "is invalid" | The reason for invalidity. |
#### InvalidPropertyTypeError
Thrown in case an invalid or unsupported value type for an object property.
##### new InvalidPropertyTypeError(objName, propName, expected, value)
| Param | Type | Description |
| --- | --- | --- |
| objName | string | The name of the object in question. |
| propName | string | The property name assigned value of invalid type. |
| expected | Array<string> \| string | The property type(s) expected. |
| value | unknown | The actual property value of invalid type assigned. |
#### InvalidReturnPropertyValueError
Thrown in case a function does not provide a valid value for one of
its returned object properties on execution.
**Kind**: global class
**See**: https://nodejs.org/api/errors.html#ERR_INVALID_RETURN_PROPERTY
##### new InvalidReturnPropertyValueError(funcName, propName, value, reason)
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| funcName | string | | The name of the function returning the invalidity. |
| propName | string | | The property name assigned the invalid value. |
| value | unknown | | The actual invalid property value assigned. |
| reason | string | "is invalid" | The reason for invalidity. |
#### InvalidReturnPropertyTypeError
Thrown in case a function does not provide an expected value type for
one of its returned object properties on execution.
**Kind**: global class
**See**: https://nodejs.org/api/errors.html#ERR_INVALID_RETURN_PROPERTY_VALUE
##### new InvalidReturnPropertyTypeError(funcName, propName, expected, value)
| Param | Type | Description |
| --- | --- | --- |
| funcName | string | The name of the function returning the invalidity. |
| propName | string | The property name assigned value of invalid type. |
| expected | Array<string> \| string | The property type(s) expected. |
| value | unknown | The actual property value of invalid type assigned. |
#### InvalidReturnValueError
Thrown in case a function does not return an expected valid value on
execution.
**Kind**: global class
**See**: https://nodejs.org/api/errors.html#ERR_INVALID_RETURN_VALUE
##### new InvalidReturnValueError(funcName, value, reason)
| Param | Type | Description |
| --- | --- | --- |
| funcName | string | The name of the function returning the invalidity. |
| value | unknown | The actual invalid value returned. |
| reason | string | The reason for invalidity. |
#### InvalidReturnTypeError
Thrown in case a function does not return an expected value type on
execution, such as when a function is expected to return a promise.
**Kind**: global class
**See**: https://nodejs.org/api/errors.html#ERR_INVALID_RETURN_VALUE
##### new InvalidReturnTypeError(funcName, expected, value)
| Param | Type | Description |
| --- | --- | --- |
| funcName | string | The name of the function returning the invalidity. |
| expected | Array<string> \| string | The return type(s) expected. |
| value | unknown | The actual value of invalid type returned. |
#### InvalidArgsNumberError
Thrown in case the number of arguments passed to a function is invalid.
##### new InvalidArgsNumberError(funcName, expected, value)
| Param | Type | Description |
| --- | --- | --- |
| funcName | string | The name of the function in question. |
| expected | number | The number of arguments expected to be passed. |
| value | number | The actual number of arguments passed. |
#### MissingOptionError
For APIs that accept options objects, some options might be mandatory. This
error is thrown if a required option is missing.
**Kind**: global class
**See**: https://nodejs.org/api/errors.html#ERR_MISSING_OPTION
##### new MissingOptionError(optName)
| Param | Type | Description |
| --- | --- | --- |
| optName | string | The name of the missing option. |
#### MissingArgsError
Thrown in case a required argument of an API was not passed.
This is only used for strict compliance with the API specification (which in
some cases may accept `func(undefined)` but not `func()`). In most native
Node.js APIs, `func(undefined)` and `func()` are treated identically, and the
`ERR_INVALID_ARG_TYPE` error code may be used instead.
**Kind**: global class
**See**: https://nodejs.org/api/errors.html#ERR_MISSING_ARGS
##### new MissingArgsError(...args)
| Param | Type | Description |
| --- | --- | --- |
| ...args | Array<string> | The names of the missing arguments. |
#### UnhandledErrorError
Thrown in case an unhandled error occurred (for instance, when an 'error'
event is emitted by an EventEmitter without an 'error' handler registered).
**Kind**: global class
**See**: https://nodejs.org/api/errors.html#ERR_UNHANDLED_ERROR
---
### Contributing
Pull requests are welcome. For major changes, please open an issue first to
discuss what you would like to change. If for whatever reason you spot something
to fix but cannot patch it yourself, please [open an issue][].
### License
This project is licensed under either of
- [Apache License, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0)
- [MIT license](https://opensource.org/licenses/MIT)
at your option.
The [SPDX](https://spdx.dev) license identifier for this project is
`MIT OR Apache-2.0`.
---
### Show Your Support
If you like the project (or want to bookmark it) —
— [give it a star ⭐️][] — it will greatly encourage
us.
[conventional-commits-badge]: https://img.shields.io/badge/commit%20style-Conventional-%23fa6673?logoColor=white&logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAzMCAzMCI+PHBhdGggc3R5bGU9ImZpbGw6ICNGRkYiIGQ9Ik0xNSwyQTEzLDEzLDAsMSwxLDIsMTUsMTMsMTMsMCwwLDEsMTUsMm0wLTJBMTUsMTUsMCwxLDAsMzAsMTUsMTUsMTUsMCwwLDAsMTUsMFoiLz48L3N2Zz4K 'Commit Style: Conventional Commits'
[conventional-commits-url]: https://www.conventionalcommits.org 'Commit Style: Conventional Commits'
[give it a star ⭐️]: https://github.com/OpenINF/openinf-util-errors/stargazers
[license-badge--shields]: https://img.shields.io/badge/license-MIT%2FApache--2.0-blue.svg?logo=github 'License: MIT/Apache 2.0'
[license-badge-url]: #license 'License: MIT/Apache 2.0'
[matrix-badge--shields]: https://img.shields.io/badge/matrix-join%20chat-%2346BC99?logo=matrix 'Chat on Matrix'
[matrix-url]: https://matrix.to/#/#openinf-space:matrix.org 'You're invited to talk on Matrix'
[npm-badge--shields]: https://img.shields.io/npm/v/@openinf/util-errors/latest.svg?logo=npm&color=fe7d37 'View on npm'
[npm-badge-url]: https://www.npmjs.com/package/@openinf/util-errors#top 'View on npm'
[open an issue]: https://github.com/OpenINF/openinf-util-errors/issues
[prettier-badge]: https://img.shields.io/badge/code_style-Prettier-ff69b4.svg?logo=prettier 'Code Style: Prettier'
[prettier-url]: https://prettier.io/playground 'Code Style: Prettier'
[project-status-badge]: https://img.shields.io/badge/project%20status-under%20construction-orange 'Project Status: Under construction badge'