pretty-error
A small tool to see node.js errors with less clutter:
... which is more readable compared to node's unformatted errors:
Installation
Install with npm:
$ npm install pretty-error
Usage and Examples
To see an error rendered with colors, you can do this:
var PrettyError = require('pretty-error');
var pe = new PrettyError();
var renderedError = pe.render(new Error('Some error message'));
console.log(renderedError);
Of course, you can render caught exceptions too:
try {
doSomethingThatThrowsAnError();
} catch (error) {
console.log(pe.render(error));
}
But if you want pretty-error to render all errors, there is a shortcut for it:
require('pretty-error').start();
... which is essentially equal to:
var PrettyError = require('pretty-error');
var pe = new PrettyError();
pe.start();
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:
pe = require('pretty-error').start();
pe.appendStyle({
'pretty-error > header > title > kind': {
display: 'none'
},
'pretty-error > header > colon': {
display: 'none'
},
'pretty-error > header > message': {
color: 'bright-white',
background: 'cyan',
padding: '0 1'
},
'pretty-error > trace > item': {
marginLeft: 2,
bullet: '"<grey>o</grey>"'
},
'pretty-error > trace > item > header > pointer > file': {
color: 'bright-cyan'
},
'pretty-error > trace > item > header > pointer > colon': {
color: 'cyan'
},
'pretty-error > trace > item > header > pointer > line': {
color: 'bright-cyan'
},
'pretty-error > trace > item > header > what': {
color: 'bright-white'
},
'pretty-error > trace > item > footer > addr': {
display: 'none'
}
});
This is how our minimal theme will look like:
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:
PrettyError = require('pretty-error');
pe = new PrettyError();
pe = require('pretty-error').start();
Shortening paths
You might want to substitute long paths with shorter, more readable aliases:
pe.alias('E:/open-source/theatrejs/lib', '(Theare.js)');
Skipping packages
You might want to skip trace lines that belong to specific packages (chai, when, socket.io):
pe.skipPackage('chai', 'when', 'socket.io');
Skipping node files
pe.skipNodeFiles();
Skipping paths
pe.skipPath('/home/dir/someFile.js');
Skipping by callback
You can customize which trace lines get logged and which won't:
pe.skip(function(traceLine, lineNumber){
if (typeof traceLine.packageName !== 'undefined' && traceLine.packageName !== 'demo') {
return true;
}
});
Modifying each trace line's contents
pe.filter(function(traceLine, lineNumber){
if (typeof traceLine.what !== 'undefined'){
traceLine.what = traceLine.what.replace(
/(.*\.module\.exports\.)(.*)/, '$2'
);
}
});
Disabling colors
pe.withoutColors();
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:
var express = require('express');
var PrettyError = require('pretty-error');
var app = express();
app.get('/', function(req, res) {
var a = b;
});
var server = app.listen(3000, function(){
console.log('Server started \n');
});
pe = new PrettyError();
app.use(function(err, req, res, next){
console.log(pe.render(err));
next();
});
pe.skipNodeFiles();
pe.skipPackage('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:
var PrettyError = require('pretty-error');
var pe = new PrettyError();
process.on('uncaughtException', function(error){
console.log(pe.render(error));
});
process.on('unhandledRejection', function(reason){
console.log("Unhandled rejection");
console.log(pe.render(reason));
});
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:
throw new Error();
process.nextTick(function(){
throw new Error();
});
License
MIT