You can define an error message via the first argument at the BaseError constructor. Additionally, BaseError allows you
to define a message format. It means that message can be generated based on arguments, passed to the error class
constructor. To achieve this, you need to override the protected format method with an inheriting class.
The message field always returns the result of invoking the format method. By default, the method returns a string that is passed
to BaseError's constructor as the first argument.
It's possible to pass an error as the second argument to the BaseError constructor. In this case, it's considered
as the error that caused the new one. After this, the error caused can be accessed via the read-only cause property.
core/error
The module provides the base class for any errors. All classes of errors should extend this one.
Reason
When TypeScript's target is < ES6, the transpiled code breaks inheritance from built-in classes. For example:
So
instanceof
won't work as expected. The same goes to a class that extendsMyError
:instanceof
returnstrue
only for theError
class.BaseError
fixes this problem. Moreover,BaseError
takes care ofname
field and sets it correctly in the constructor.How to use
Additional abilities
Message format
You can define an error message via the first argument at the
BaseError
constructor. Additionally,BaseError
allows you to define a message format. It means that message can be generated based on arguments, passed to the error class constructor. To achieve this, you need to override the protectedformat
method with an inheriting class.The
message
field always returns the result of invoking theformat
method. By default, the method returns a string that is passed toBaseError
's constructor as the first argument.Causing error
It's possible to pass an error as the second argument to the
BaseError
constructor. In this case, it's considered as the error that caused the new one. After this, the error caused can be accessed via the read-onlycause
property.