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
instanceofwon't work as expected. The same goes to a class that extendsMyError:instanceofreturnstrueonly for theErrorclass.BaseErrorfixes this problem. Moreover,BaseErrortakes care ofnamefield 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
BaseErrorconstructor. Additionally,BaseErrorallows 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 protectedformatmethod with an inheriting class.The
messagefield always returns the result of invoking theformatmethod. 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
BaseErrorconstructor. 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-onlycauseproperty.