pretty-exceptions

Pretty and more helpful uncaught exceptions, automatically.

Pretty Exceptions

Pretty and more helpful uncaught exceptions, automatically.

Usage

Library

const pretty = require('pretty-exceptions/lib')

const options = {
  source: true,
  native: true,
  color: true,
  cwd: process.cwd()
}

const output = pretty(new Error('foo'), options)

options

nametyperequireddefaultdescription
sourceBooleanfalseShow source code in stack
nativeBooleanfalseShow native code in stack
colorBooleantrueuse ansi colors
cwdStringprocess.cwd()resolve file names relative to this path

CLI (preferred)

This is the preferred approach to best avoid mistakingly including into production builds

  1. setup your NODE_PATHenvironment variable
export NODE_PATH="$NODE_PATH:$(npm root -g)"
  1. install pretty-exceptions
npm install --global pretty-exceptions
  1. run your application with the --requireflag:
node --require pretty-exceptions my-app.js

# alternative modes (see below):
node --require pretty-exceptions/source my-app.js

Pro Tip:

Use Bash Aliases for less typing

# make a special alias for debugging
alias node-ex='node --require pretty-exceptions'

# or even better:
alias node='node --require pretty-exceptions/source'

To make this persistent, you need to add this into your ~/.bashrc(or ~/.bash_profile).

Environment Variables

envdefaultdescription
PRETTY_EXCEPTIONS_SOURCE0Show source code in stack
PRETTY_EXCEPTIONS_NATIVE0Show native code in stack
PRETTY_EXCEPTIONS_COLOR1use ansi colors

Source (if you really must!)

Require at the top-most entry point of your app:

require('pretty-exceptions')

// alternative modes (see below):
require('pretty-exceptions/source-native')

Modes

Default

$ node --require pretty-exceptions my-app.js
Output
Error: oh no! this is an error message!
 │
 └┬╼ /path/to/my-app.js
  │
  ├──╼ someOtherFunction @ line 2:27
  ├──╼ someFunction @ line 6:3
  └──╼ Object.<anonymous> @ line 9:1
Demo

Colors will vary based on your local terminal configuration

View Source

$ node --require pretty-exceptions/source my-app.js
# OR
$ PRETTY_EXCEPTIONS_SOURCE=1 node --require pretty-exceptions my-app.js
Output
Error: oh no! this is an error message!
 │
 └┬╼ /path/to/my-app.js
  │
  ├──╼ someOtherFunction @ line 2
  │
  │    function someOtherFunction () {
  ├╌╌╌╌╌╌╌╌╌╌╮
  │    throw new Error('oh no! this is an error message!')
  │    }
  │
  ├──╼ someFunction @ line 6
  │
  │    function someFunction () {
  ├╌╌╌╌╮
  │    someOtherFunction()
  │    }
  │
  ├──╼ Object.<anonymous> @ line 9
  │
  └╌╌╌╌╮
       someFunction()
Demo

Colors will vary based on your local terminal configuration

View Native Calls

$ node --require pretty-exceptions/native my-app.js
# OR
$ PRETTY_EXCEPTIONS_NATIVE=true node --require pretty-exceptions my-app.js
Output
Error: oh no! this is an error message!
 │
 ├─┬╼ /path/to/my-app.js
 │ │
 │ ├──╼ someOtherFunction @ line 2:27
 │ ├──╼ someFunction @ line 6:3
 │ └──╼ Object.<anonymous> @ line 9:1
 │
 ├─┬╼ module.js
 │ │
 │ ├──╼ Module._compile @ line 571:32
 │ ├──╼ Object.Module._extensions..js @ line 580:10
 │ ├──╼ Module.load @ line 488:32
 │ ├──╼ tryModuleLoad @ line 447:12
 │ ├──╼ Function.Module._load @ line 439:3
 │ └──╼ Module.runMain @ line 605:10
 │
 └┬╼ bootstrap_node.js
  │
  └──╼ run @ line 423:7
Demo

Colors will vary based on your local terminal configuration

View Source & Native

$ node --require pretty-exceptions/source-native my-app.js
# OR
$ PRETTY_EXCEPTIONS_SOURCE=1 PRETTY_EXCEPTIONS_NATIVE=1 node --require pretty-exceptions my-app.js
Output
Error: oh no! this is an error message!
 │
 ├─┬╼ /path/to/my-app.js
 │ │
 │ ├──╼ someOtherFunction @ line 2
 │ │
 │ │    function someOtherFunction () {
 │ ├╌╌╌╌╌╌╌╌╌╌╮
 │ │    throw new Error('oh no! this is an error message!')
 │ │    }
 │ │
 │ ├──╼ someFunction @ line 6
 │ │
 │ │    function someFunction () {
 │ ├╌╌╌╌╮
 │ │    someOtherFunction()
 │ │    }
 │ │
 │ ├──╼ Object.<anonymous> @ line 9
 │ │
 │ └╌╌╌╌╮
 │      someFunction()
 │ 
 │
 ├─┬╼ module.js
 │ │
 │ ├──╼ Module._compile @ line 571
 │ ├──╼ Object.Module._extensions..js @ line 580
 │ ├──╼ Module.load @ line 488
 │ ├──╼ tryModuleLoad @ line 447
 │ ├──╼ Function.Module._load @ line 439
 │ └──╼ Module.runMain @ line 605
 │
 └┬╼ bootstrap_node.js
  │
  └──╼ run @ line 423
Demo

Colors will vary based on your local terminal configuration


License: ISC• Copyright: ahmadnassri.com• Github: @ahmadnassri• Twitter: @ahmadnassri

HomePage

https://github.com/ahmadnassri/pretty-exceptions

Repository

https://github.com/ahmadnassri/pretty-exceptions.git


上一篇:markdown-it-vanilla-loader
下一篇:jest-wrap

相关推荐

  • 纯css3漂亮的checkbox和radio美化效果插件Pretty.css插件

    插件介绍 pretty.css是一款纯css3漂亮的checkbox和radio美化效果。pretty.css可以和多种字体图标结合使用,对原生的checkbox和radio进行美化,还可以制作按...

    9 个月前
  • 【每日一包0017】pretty-ms

    github地址:https://github.com/ABCDdouyae...(https://github.com/ABCDdouyaer/apackperdayNO.1) prettyms...

    1 年前
  • vfile-reporter-pretty

    Pretty reporter for VFile vfilereporterpretty Buildbuildbadgebuild Coveragecoveragebadgecoverage ...

    2 年前
  • stylelint-formatter-pretty

    A pretty Stylelint formatter. stylelintformatterpretty A pretty formatter for Stylelint(https://...

    1 年前
  • start-pretty-reporter

    Pretty reporter for Start startprettyreporter npm(https://img.shields.io/npm/v/startprettyreporte...

    2 年前
  • spdx-exceptions

    list of SPDX standard license exceptions The package exports an array of strings. Each string is an...

    2 年前
  • rollup-plugin-prettyuglify

    Rollup plugin to minify generated bundle into the shape of an SVG's paths rolluppluginprettyuglify...

    1 年前
  • prettyugly

    Pretty/uglify CSS Uglify (strip spaces) or prettify (add consistent spaces) CSS code "Ambition...

    9 个月前
  • prettysize

    Convert bytes to other sizes for prettier logging PrettySize ========== Helper utility to provide ...

    1 年前
  • prettylint

    Run Prettier as a linter prettylint npm(https://img.shields.io/npm/v/prettylint.svg)(https://www....

    2 年前

官方社区

扫码加入 JavaScript 社区