Package Exports
- @openinf/util-errors
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@openinf/util-errors) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@openinf/util-errors
Essential utility errors inspired by Node.js core error codes
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. As is the case with any software project in continuous
development, omissions and errors may exist, for which contributions are
welcome.
Installation
@openinf/util-errors
runs on Node.js and is available via npm
.
npm install @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.
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.
Classes
- 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 accept
func(undefined)
but notfunc()
). In most native Node.js APIs,func(undefined)
andfunc()
are treated identically, and theERR_INVALID_ARG_TYPE
error 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
- https://github.com/nodejs/node/blob/8c9dc4e9e65af92c9b66bbbe1b001430d9110cd9/lib/internal/errors.js#L1121-L1128
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
- https://github.com/nodejs/node/blob/8c9dc4e9e65af92c9b66bbbe1b001430d9110cd9/lib/internal/errors.js#L1014-L1120
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
- https://github.com/nodejs/node/blob/8c9dc4e9e65af92c9b66bbbe1b001430d9110cd9/lib/internal/errors.js#L1187-L1190
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
- https://github.com/nodejs/node/blob/8c9dc4e9e65af92c9b66bbbe1b001430d9110cd9/lib/internal/errors.js#L1191-L1200
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
- https://github.com/nodejs/node/blob/8c9dc4e9e65af92c9b66bbbe1b001430d9110cd9/lib/internal/errors.js#L1201-L1210
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
- https://github.com/nodejs/node/blob/8c9dc4e9e65af92c9b66bbbe1b001430d9110cd9/lib/internal/errors.js#L1201-L1210
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
- https://github.com/nodejs/node/blob/8c9dc4e9e65af92c9b66bbbe1b001430d9110cd9/lib/internal/errors.js#L1294
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
- https://github.com/nodejs/node/blob/8c9dc4e9e65af92c9b66bbbe1b001430d9110cd9/lib/internal/errors.js#L1268-L1293
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
- https://github.com/nodejs/node/blob/8c9dc4e9e65af92c9b66bbbe1b001430d9110cd9/lib/internal/errors.js#L1454-L1461
© OpenINF