Installations
npm install pretty-error
Score
55.5
Supply Chain
98.6
Quality
75.7
Maintenance
100
Vulnerability
99.6
License
Releases
Unable to fetch releases
Developer
AriaMinaei
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
No
Node Version
16.2.0
NPM Version
7.13.0
Statistics
1,521 Stars
110 Commits
49 Forks
21 Watching
5 Branches
9 Contributors
Updated on 20 Nov 2024
Bundle Size
226.04 kB
Minified
56.87 kB
Minified + Gzipped
Languages
CoffeeScript (99.82%)
JavaScript (0.18%)
Total Downloads
Cumulative downloads
Total Downloads
2,754,174,878
Last day
-4.1%
2,287,364
Compared to previous day
Last week
3.2%
12,074,006
Compared to previous week
Last month
12%
50,497,342
Compared to previous month
Last year
-13.8%
581,716,291
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
5
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:
1var PrettyError = require('pretty-error'); 2var pe = new PrettyError(); 3 4var renderedError = pe.render(new Error('Some error message')); 5console.log(renderedError);
Of course, you can render caught exceptions too:
1try { 2 doSomethingThatThrowsAnError(); 3} catch (error) { 4 console.log(pe.render(error)); 5}
But if you want pretty-error to render all errors, there is a shortcut for it:
1require('pretty-error').start();
... which is essentially equal to:
1var PrettyError = require('pretty-error'); 2 3// instantiate PrettyError, which can then be used to render error objects 4var pe = new PrettyError(); 5pe.start();
You can also preload pretty-error into your code using node's --require
argument:
$ node --require pretty-error/start your-module.js
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:
1// the start() shortcut returns an instance of PrettyError ... 2pe = require('pretty-error').start(); 3 4// ... which we can then use to customize like this: 5pe.appendStyle({ 6 // this is a simple selector to the element that says 'Error' 7 'pretty-error > header > title > kind': { 8 // which we can hide: 9 display: 'none' 10 }, 11 12 // the 'colon' after 'Error': 13 'pretty-error > header > colon': { 14 // we hide that too: 15 display: 'none' 16 }, 17 18 // our error message 19 'pretty-error > header > message': { 20 // let's change its color: 21 color: 'bright-white', 22 23 // we can use black, red, green, yellow, blue, magenta, cyan, white, 24 // grey, bright-red, bright-green, bright-yellow, bright-blue, 25 // bright-magenta, bright-cyan, and bright-white 26 27 // we can also change the background color: 28 background: 'cyan', 29 30 // it understands paddings too! 31 padding: '0 1' // top/bottom left/right 32 }, 33 34 // each trace item ... 35 'pretty-error > trace > item': { 36 // ... can have a margin ... 37 marginLeft: 2, 38 39 // ... and a bullet character! 40 bullet: '"<grey>o</grey>"' 41 42 // Notes on bullets: 43 // 44 // The string inside the quotation mark gets used as the character 45 // to show for the bullet point. 46 // 47 // You can set its color/background color using tags. 48 // 49 // This example sets the background color to white, and the text color 50 // to cyan, the character will be a hyphen with a space character 51 // on each side: 52 // example: '"<bg-white><cyan> - </cyan></bg-white>"' 53 // 54 // Note that we should use a margin of 3, since the bullet will be 55 // 3 characters long. 56 }, 57 58 'pretty-error > trace > item > header > pointer > file': { 59 color: 'bright-cyan' 60 }, 61 62 'pretty-error > trace > item > header > pointer > colon': { 63 color: 'cyan' 64 }, 65 66 'pretty-error > trace > item > header > pointer > line': { 67 color: 'bright-cyan' 68 }, 69 70 'pretty-error > trace > item > header > what': { 71 color: 'bright-white' 72 }, 73 74 'pretty-error > trace > item > footer > addr': { 75 display: 'none' 76 } 77});
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:
1PrettyError = require('pretty-error'); 2pe = new PrettyError(); 3 4// or: 5pe = require('pretty-error').start();
Shortening paths
You might want to substitute long paths with shorter, more readable aliases:
1pe.alias('E:/open-source/theatrejs/lib', '(Theatre.js)');
Skipping packages
You might want to skip trace lines that belong to specific packages (chai, when, socket.io):
1pe.skipPackage('chai', 'when', 'socket.io');
Skipping node files
1// this will skip node.js, path.js, event.js, etc. 2pe.skipNodeFiles();
Skipping paths
1pe.skipPath('/home/dir/someFile.js');
Skipping by callback
You can customize which trace lines get logged and which won't:
1pe.skip(function(traceLine, lineNumber){ 2 // if we know which package this trace line comes from, and it isn't 3 // our 'demo' package ... 4 if (typeof traceLine.packageName !== 'undefined' && traceLine.packageName !== 'demo') { 5 // then skip this line 6 return true; 7 } 8 9 // You can console.log(traceLine) to see all of it's properties. 10 // Don't expect all these properties to be present, and don't assume 11 // that our traceLine is always an object. 12});
Modifying each trace line's contents
1pe.filter(function(traceLine, lineNumber){ 2 // the 'what' clause is something like: 3 // 'DynamicTimeline.module.exports.DynamicTimeline._verifyProp' 4 if (typeof traceLine.what !== 'undefined'){ 5 6 // we can shorten it with a regex: 7 traceLine.what = traceLine.what.replace( 8 /(.*\.module\.exports\.)(.*)/, '$2' 9 ); 10 } 11});
Disabling colors
1pe.withoutColors(); // Errors will be rendered without coloring
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:
1// this is app.js 2 3var express = require('express'); 4var PrettyError = require('pretty-error'); 5 6var app = express(); 7 8app.get('/', function(req, res) { 9 // this will throw an error: 10 var a = b; 11}); 12 13var server = app.listen(3000, function(){ 14 console.log('Server started \n'); 15}); 16 17 18// we can now instantiaite Prettyerror: 19pe = new PrettyError(); 20 21// and use it for our app's error handler: 22app.use(function(err, req, res, next){ 23 console.log(pe.render(err)); 24 next(); 25}); 26 27// we can optionally configure prettyError to simplify the stack trace: 28 29pe.skipNodeFiles(); // this will skip events.js and http.js and similar core node files 30pe.skipPackage('express'); // this will skip all the trace lines about express` core and sub-modules
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:
1var PrettyError = require('pretty-error'); 2var pe = new PrettyError(); 3 4// To render exceptions thrown in non-promies code: 5process.on('uncaughtException', function(error){ 6 console.log(pe.render(error)); 7}); 8 9// To render unhandled rejections created in BlueBird: 10process.on('unhandledRejection', function(reason){ 11 console.log("Unhandled rejection"); 12 console.log(pe.render(reason)); 13}); 14 15// While PrettyError.start() works out of the box with when.js` unhandled rejections, 16// now that wer'e manually rendering errors, we have to instead use npmjs.org/packages/pretty-monitor 17// to handle when.js rejections. 18
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:
1// (continued form above) 2 3throw new Error(); // not prettified 4process.nextTick(function(){ 5 throw new Error(); // prettified 6}); 7
License
MIT
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
4 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-qrpm-p2h7-hrv2
Reason
Found 4/24 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/main.yml:1
- Info: no jobLevel write permissions found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:18: update your workflow using https://app.stepsecurity.io/secureworkflow/AriaMinaei/pretty-error/main.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:20: update your workflow using https://app.stepsecurity.io/secureworkflow/AriaMinaei/pretty-error/main.yml/master?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/main.yml:24
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 10 are checked with a SAST tool
Score
3.2
/10
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to pretty-error
youch
HTML Pretty error stack viewer
@nuxtjs/youch
Pretty error reporting for Node.js 🚀 (Modified for Nuxt.js & SSR Bundles)
nestjs-flub
Pretty Error Stack Viewer for NestJS Framework
usnam-pmb
Concise stack traces for node: pretty-error + compact theme (still with paths) + auto-start.