Exceptions for Kernel Framework
npm install @kernel-js/exceptionsnpm install @kernel/exceptions`
Class Directory
$3
* AlreadyInUseError
* ArgumentError
* ArgumentNullError
* AuthenticationRequiredError
* ConnectionError
* DataError
* ForbiddenError
* HttpError
* InvalidOperationError
* SocketError
* NotFoundError
* NotImplementedError
* NotPermittedError
* NotSupportedError
* RangeError
* ReferenceError
* SyntaxError
* TimeoutError
* TypeError
* URIError
Error Classes
$3
Applicable when a resource is already in use, for example unique key constraints like a username.
new AlreadyInUseError(entityName, [arg1, arg2, arg3, ..., error])
__Arguments__
* entityName - the entity that owns the protected resource
* args - the fields or attributes that are already in use
* error - the Error instance that caused the current error. Stack trace will be appended
`js
// Example
throw new AlreadyInUseError('user', 'username');
`
---------------------------------------
$3
Applicable when there's a generic problem with an argument received by a function call.
new ArgumentError(argumentName[, error])
__Arguments__
* argumentName - the name of the argument that has a problem
* error - the Error instance that caused the current error. Stack trace will be appended
`js
// Example
throw new ArgumentError('username', 500, err);
`
---------------------------------------
$3
Applicable when an argument received by a function call is null/undefined or empty.
new ArgumentNullError(message[, code, error])
__Arguments__
* message - any message
* code - the error code
* error - the Error instance that caused the current error. Stack trace will be appended
`js
// Example
throw new ArgumentNullError('username', 400, err);
`
---------------------------------------
$3
Applicable when an operation requires authentication
new AuthenticationRequiredError(message, [code, error])
__Arguments__
* message - any message
* code - the error code
* error - the Error instance that caused the current error. Stack trace will be appended
`js
// Example
throw new AuthenticationRequiredError("Please provide authentication.", 403, err);
`
---------------------------------------
$3
Applicable when an error occurs on a connection.
new ConnectionError(message[, code, error])
__Arguments__
* message - any message
* code - the error code
* error - the Error instance that caused the current error. Stack trace will be appended
`js
// Example
throw new ConnectionError('WebService unavailable');
throw new ConnectionError('Database connection no longer available', 500, err);
`
---------------------------------------
$3
Applicable when an error occurs on or with an external data source.
new DataError(message[, code, error])
__Arguments__
* message - any message
* code - the error code
* error - the Error instance that caused the current error. Stack trace will be appended
`js
// Example
throw new DataError('Too many rows returned from database', 500, err);
`
---------------------------------------
$3
Applicable when an operation is not permitted
new ForbiddenError(message[, code, error])
__Arguments__
* message - any message
* code - the error code
* error - the Error instance that caused the current error. Stack trace will be appended
`js
// Example
throw new NotPermittedError("username cannot be changed once set.", 403, err);
`
---------------------------------------
$3
Represents a message and a HTTP status code.
new HttpError(code[, message, codeMap, error])
__Arguments__
* code - any HTTP status code integer
* message - any message
* codeMap - the mapping of error codes and their classes
* error - the Error instance that caused the current error. Stack trace will be appended
`js
// Default example
throw new HttpError(404);
// Example with error
throw new HttpError(404, err);
// Example with message and error
throw new HttpError(404, "Not Found", err);
// Example with custom codeMap and error
throw new HttpError(404, {
400: 'ArgumentError',
401: 'AuthenticationRequiredError',
403: 'ForbiddenError',
404: 'NotFoundError',
405: 'NotSupportedError',
409: 'AlreadyInUseError',
}, err);
`
---------------------------------------
$3
Applicable when an invalid operation occurs.
new InvalidOperationError(message[, code, error])
__Arguments__
* message - any message
* code - the error code
* error - the Error instance that caused the current error. Stack trace will be appended
`js
// Example
throw new InvalidOperationError('Divide by zero', 500, err);
`
---------------------------------------
$3
Applicable when an attempt to retrieve data yielded no result.
new NotFoundError(entity_name[, code, error])
__Arguments__
* entity_name - a description for what was not found
* code - the error code
* error - the Error instance that caused the current error. Stack trace will be appended
`js
// Example
throw new NotFoundError("User", 500, err);
`
---------------------------------------
$3
Applicable when a requested method or operation is not implemented.
new NotImplementedError(message[, code, error])
__Arguments__
* message - any message
* code - the error code
* error - the Error instance that caused the current error. Stack trace will be appended
`js
// Example
throw new NotImplementedError("Method is not yet implemented.", 500, err);
`
---------------------------------------
$3
Applicable when a certain condition is not supported by your application.
new NotSupportedError(message[, code, error])
__Arguments__
* message - a message
* code - the error code
* error - the Error instance that caused the current error. Stack trace will be appended
`js
// Example
throw new NotSupportedError('Zero values', 500, err);
`
---------------------------------------
$3
Represents an error that occurs when a numeric variable or parameter is outside of its valid range. This is roughly the same as the native RangeError class. It additionally supports an code attribute.
new RangeError(message[, code, error])
__Arguments__
* message - a message
* code - the error code
* error - the Error instance that caused the current error. Stack trace will be appended
`js
// Example
throw new RangeError("Value must be between " + MIN + " and " + MAX, err);
`
---------------------------------------
$3
Represents an error when a non-existent variable is referenced. This is roughly the same as the native ReferenceError class. It additionally supports an code attribute.
new ReferenceError(message[, code, error])
__Arguments__
* message - a message
* code - the error code
* error - the Error instance that caused the current error. Stack trace will be appended
`js
// Example
throw new ReferenceError("x is not defined", 500, err);
`
---------------------------------------
$3
Applicable when an error occurs on a socket.
new SocketError(message[, code, error])
__Arguments__
* message - any message
* code - the error code
* error - the Error instance that caused the current error. Stack trace will be appended
`js
// Example
throw new SocketError('Socket no longer available', 500, err);
`
---------------------------------------
$3
Represents an error when trying to interpret syntactically invalid code. This is roughly the same as the native SyntaxError class. It additionally supports an code attribute.
new SyntaxError(message[, code, error])
__Arguments__
* message - a message
* code - the error code
* error - the Error instance that caused the current error. Stack trace will be appended
`js
// Example
throw new SyntaxError("Unexpected token a", 500, err);
`
---------------------------------------
$3
Applicable when an operation takes longer than the alloted amount.
new TimeoutError(time[, code, error])
__Arguments__
* time - a time duration
* code - the error code
* error - the Error instance that caused the current error. Stack trace will be appended
`js
// Example
throw new TimeoutError('100ms', 500, err);
`
---------------------------------------
$3
Represents an error when a value is not of the expected type. This is roughly the same as the native TypeError class. It additionally supports an code attribute.
new TypeError(message[, code, error])
__Arguments__
* message - a message
* code - the error code
* error - the Error instance that caused the current error. Stack trace will be appended
`js
// Example
throw new TypeError("number is not a function", 500, err);
`
---------------------------------------
$3
Represents an error when a value is not of the expected type. This is roughly the same as the native URIError class.
It additionally supports an code attribute.
new URIError(message[, code, error])
__Arguments__
* message - a message
* code - the error code
* error - the Error instance that caused the current error. Stack trace will be appended
`js
// Example
throw new URIError("URI malformed", 500, err);
``