Is there any harm in updating the message of an Error object like this?
const err = new Error('bar');
...
err.message = `foo ${err.message}`;
My objective is to add some useful information to the error message when logging the error.
It can be useful to add some extra information/breadcrumbs as an exception travels up through your application layers. That said, you are mutating an object, which can be hard to reason about in a large code-base; exceptions management normally being a cross-cutting concern in your application.
Also bear in mind that some libraries will extend the Error
class and leave the message
property without a setter, making it ready-only.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With