pretty-error

Dependency status Build Status npm

A small tool to see node.js errors with less clutter:

screenshot of pretty-error

... which is more readable compared to node's unformatted errors:

screenshot of normal errors

Installation

Install with npm:

Usage and Examples

To see an error rendered with colors, you can do this:

Of course, you can render caught exceptions too:

But if you want pretty-error to render all errors, there is a shortcut for it:

... which is essentially equal to:

You can also preload pretty-error into your code using node's --require argument:

How it Works

PrettyError turns error objects into something similar to an html document, and then uses RenderKid to render the document using simple html/css-like commands. This allows PrettyError to be themed using simple css-like declarations.

Theming

PrettyError's default theme is a bunch of simple css-like rules. Here is the source of the default theme.

Since the default theme is all css, you can customize it to fit your taste. Let's do a minimal one:

This is how our minimal theme will look like: screenshot of our custom theme

Read RenderKid's docs to learn about all the css rules that are supported.

Customization

There are a few methods to help you customize the contents of your error logs.

Let's instantiate first:

Shortening paths

You might want to substitute long paths with shorter, more readable aliases:

Skipping packages

You might want to skip trace lines that belong to specific packages (chai, when, socket.io):

Skipping node files

Skipping paths

Skipping by callback

You can customize which trace lines get logged and which won't:

Modifying each trace line's contents

Disabling colors

Integrating with frameworks

PrettyError is very simple to set up, so it should be easy to use within other frameworks.

Integrating with express

Most frameworks such as express, catch errors automatically and provide a mechanism to handle those errors. Here is an example of how you can use PrettyError to log unhandled errors in express:

Troubleshooting

PrettyError.start() modifies the stack traces of all errors thrown anywhere in your code, so it could potentially break packages that rely on node's original stack traces. I've only encountered this problem once, and it was with BlueBird when Promise.longStackTraces() was on.

In order to avoid this problem, it's better to not use PrettyError.start() and instead, manually catch errors and render them with PrettyError:

The only drawback with this approach is that exceptions thrown in the first tick are not prettified. To fix that, you can delay your application's startup for one tick:

License

MIT

Last updated

Was this helpful?