This library provides an enhanced Error class that extends the built-in Error functionality with features like an internal message (that can be used for logging but not returned back to the user in an API context), logging of the error at the moment it's thrown, event triggering for application reactivity during error occurrences, and defining a data object that can be logged with the message and masked. This mixin approach retains the original error class, allowing compatibility with NestJS exception filters and differentiation of errors by type.
Also, it might be useful to read the Event and Logger documentation to understand how these 2 components work since they are used in the errors library.
ON_DEFAULT_ERROR_EVENT
: The event name for default errors.
The module exports two interfaces:
-
IAbstractDefaultError
: Extends theHttpException
class with additional propertiesdata
andinternalMessage
. -
IDefaultErrorOptions
: Specifies options for the default error class. It includes the following properties:data
: An optional property that can be of any type. This property is used to specify additional data related to the error that will be logged but not thrown back to the user.masks
: An optional array of strings. These strings represent keys in the data object that should be masked in the logs for privacy or security reasons.logger
: This property can either be an instance of theImprovedLoggerService
or a boolean. If set to true, the default logger (console) is used. If an instance ofImprovedLoggerService
is provided, it will be used for logging.response
: An optional string or object that represents a message that will can be safely sent to the client.eventEmitter
: This can be an instance of eitherEventEmitter2
orEventEmitter
. It will be used to emit an event when an error occurs.
newDefaultError
: A convenience function that creates a newDefaultError
class instance extending the provided base class. It takes three parameters: a base class, options, and additional arguments.DefaultErrorMixin
: A function that returns a class extending from the provided base class (or theError
class if no base class is provided), with theIAbstractDefaultError
interface. This class can accept options and parameters in its constructor, handles logging, and emits an event on error occurrence.
DefaultErrorMixin
: A type alias for theMixin
of theDefaultErrorMixin
function.
DefaultError
: An exported class that extends the DefaultErrorMixin
class and provides a constructor with optional parameters for a message and options for both the extended DefaultError
and the base error class.
// Using IDefaultErrorOptions
const options: IDefaultErrorOptions = {
data: { username: 'john', password: 'password' },
masks: ['password'],
logger: true,
response: 'Something happened',
eventEmitter: myEventEmitter,
};
Using DefaultError class, this will throw an error based on the native Error class
but with the additional features of the DefaultError class. You can also consider to extend this
class to create your custom one, if you want your class to use the Error
class as a base (child of Error).
throw new DefaultError('Something went wrong', options);
Scenario #2. newDefaultError function - When you want to throw a different error type but wrapped by the DefaultError functionalities
This allows us to choose the base class for the error, in this case UnauthorizedException
throw newDefaultError(UnauthorizedException, options, 'Login failed');
This allows us to create a custom error class that is a mixin of the DefaultError class and extends the UnauthorizedException class. You can use this class to implement your own logic around the error handling, for example, to add additional properties or methods.
class MyCustomError extends DefaultErrorMixin(UnauthorizedException) {
constructor(message: string, options?: IDefaultErrorOptions) {
super(options ?? {}, message);
}
}
throw new MyCustomError('Login failed', options);
The library also provides specialized classes that extend the DefaultError
class and provide additional functionality. They also include HTTP codes to facilitate semantic error handling for HTTP requests. Some of these classes are:
-
BadRequestError
: This class extends theDefaultError
class and provides a constructor with optional parameters for a message and options for both the extendedBadRequestError
and the base error class. It also provides a static methodthrow
that throws aBadRequestError
with the provided message and options. -
UnauthorizedError
: This class extends theDefaultError
class and provides a constructor with optional parameters for a message and options for both the extendedUnauthorizedError
and the base error class. It also provides a static methodthrow
that throws anUnauthorizedError
with the provided message and options. -
ForbiddenError
: This class extends theDefaultError
class and provides a constructor with optional parameters for a message and options for both the extendedForbiddenError
and the base error class. It also provides a static methodthrow
that throws aForbiddenError
with the provided message and options. -
LoginError
: This class extends theDefaultError
class and provides a constructor with optional parameters for a message and options for both the extendedLoginError
and the base error class. It also provides a static methodthrow
that throws aLoginError
with the provided message and options. This is a custom class that uses the status code 401 just like theUnauthorizedError
class, but it is used to indicate that the user is not logged in.
check the error.class.ts
file for more details