@nuov.io/error
v1.1.63
Published
The @nuov.io error library.
Readme
Error
Description
Error is a lightweight TypeScript module that provides abstract and typed error classes for the @nuov.io ecosystem.
Installation
npm install @nuov.io/errorUsage
AbstractError
The AbstractError class is the base class other errors in the module. It preserves the standard Error constructor signature and assigns the name of the concrete class to the name property.
/** The class AbstractError. */
export abstract class AbstractError extends Error {
/**
* Constructor.
* @param message - the message
* @param options - the options
*/
public constructor(message?: string, options?: ErrorOptions) {
super(message, options);
this.name = this.constructor.name;
}
}
/** The class BadRequestError. */
export class BadRequestError extends AbstractError {
/** The http status code. */
public static get HTTP_STATUS_CODE(): number {
return 400;
}
}
const error: Error = new BadRequestError("Something isn't right...");
console.log(error.name); // "BadRequestError"
console.log(error.message); // "Something isn't right..."
console.log(error instanceof Error); // true
console.log(error instanceof AbstractError); // true
console.log(error instanceof ValidationError); // trueChaining causes
Because AbstractError accepts the standard ErrorOptions, errors can be chained with a cause:
import { BadRequestError } from "@nuov.io/error";
const causeError: Error = new Error("The property 'name' is blank.");
const options: ErrorOptions = { cause: causeError };
const error: Error = new BadRequestError(
"The object could not be added.",
options,
);
console.log(error.cause); // Error: "The property 'name' is blank."API Reference
AbstractError (abstract class)
Constructor
public constructor(message?: string, options?: ErrorOptions) {
// see above
}| Parameter | Type | Description |
| --------- | -------------- | ----------------------- |
| message | string | Optional error message. |
| options | ErrorOptions | Optional error options. |
HTTP status codes
| Class | Status Code |
| ------------------------------------------ | ----------- |
| BadRequestError.HTTP_STATUS_CODE | 400 |
| UnauthorizedError.HTTP_STATUS_CODE | 401 |
| ForbiddenError.HTTP_STATUS_CODE | 403 |
| NotFoundError.HTTP_STATUS_CODE | 404 |
| NotModifiedError.HTTP_STATUS_CODE | 304 |
| LockedError.HTTP_STATUS_CODE | 423 |
| ServiceUnavailableError.HTTP_STATUS_CODE | 503 |
Each class follows the same constructor signature as AbstractError:
new ErrorClass(message?: string, options?: ErrorOptions)License
MIT License
