@seudev/js-error

A lightweight library, that provides an Error class to throws errors with cause, making it easier to chaining of errors.

Usage no npm install needed!

<script type="module">
  import seudevJsError from 'https://cdn.skypack.dev/@seudev/js-error';
</script>

README

JS Error

What is it?

A lightweight library, that provides an Error class to throws errors with cause, making it easier to chaining of errors.

Using JS Error

npm i @seudev/js-error

The JSError class is a subclass of Error class. Create instances of this class passing a message and a cause (optional). The message can be an string or an object.

throw new JSError("Foo");

throw new JSError("Foo", cause);

throw new JSError({name: "ValidationError", message: "Foo"}, cause);

throw new JSError({name: "ValidationError", message: "Foo"}, cause);

throw new JSError({name: "ValidationError", message: "Foo", data: {}}, cause);

With this class you can do error handling an throws a new error passing the cause of this error.

try {
    throw new Error("Foo");
} catch(ex) {
    throw new JSError("Bar", ex);
}

This code will generate a stacktrace like this:

stacktrace

Licensing

seudev/js-error is provided and distributed under the Apache Software License 2.0.

Refer to LICENSE for more information.