source: imaps-frontend/node_modules/pretty-error/README.md@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 9.1 KB
Line 
1# pretty-error
2
3[![Dependency status](https://david-dm.org/AriaMinaei/pretty-error.svg)](https://david-dm.org/AriaMinaei/pretty-error)
4[![Build Status](https://secure.travis-ci.org/AriaMinaei/pretty-error.svg?branch=master)](https://travis-ci.org/AriaMinaei/pretty-error) [![npm](https://img.shields.io/npm/dm/pretty-error.svg)](https://npmjs.org/package/pretty-error)
5
6A small tool to see node.js errors with less clutter:
7
8![screenshot of pretty-error](https://github.com/AriaMinaei/pretty-error/raw/master/docs/images/pretty-error-screenshot.png)
9
10... which is more readable compared to node's unformatted errors:
11
12![screenshot of normal errors](https://github.com/AriaMinaei/pretty-error/raw/master/docs/images/normal-error-screenshot.png)
13
14## Installation
15
16Install with npm:
17
18 $ npm install pretty-error
19
20## Usage and Examples
21
22To see an error rendered with colors, you can do this:
23
24```javascript
25var PrettyError = require('pretty-error');
26var pe = new PrettyError();
27
28var renderedError = pe.render(new Error('Some error message'));
29console.log(renderedError);
30```
31
32Of course, you can render caught exceptions too:
33
34```javascript
35try {
36 doSomethingThatThrowsAnError();
37} catch (error) {
38 console.log(pe.render(error));
39}
40```
41
42But if you want pretty-error to render all errors, there is a shortcut for it:
43
44```javascript
45require('pretty-error').start();
46```
47
48... which is essentially equal to:
49
50```javascript
51var PrettyError = require('pretty-error');
52
53// instantiate PrettyError, which can then be used to render error objects
54var pe = new PrettyError();
55pe.start();
56```
57
58You can also preload pretty-error into your code using node's [`--require`](https://nodejs.org/api/cli.html#cli_r_require_module) argument:
59
60```
61$ node --require pretty-error/start your-module.js
62```
63
64## How it Works
65
66PrettyError turns error objects into something similar to an html document, and then uses [RenderKid](https://github.com/AriaMinaei/renderkid) to render the document using simple html/css-like commands. This allows PrettyError to be themed using simple css-like declarations.
67
68## Theming
69
70PrettyError's default theme is a bunch of simple css-like rules. [Here](https://github.com/AriaMinaei/pretty-error/blob/master/src/defaultStyle.coffee) is the source of the default theme.
71
72Since the default theme is all css, you can customize it to fit your taste. Let's do a minimal one:
73
74```javascript
75// the start() shortcut returns an instance of PrettyError ...
76pe = require('pretty-error').start();
77
78// ... which we can then use to customize like this:
79pe.appendStyle({
80 // this is a simple selector to the element that says 'Error'
81 'pretty-error > header > title > kind': {
82 // which we can hide:
83 display: 'none'
84 },
85
86 // the 'colon' after 'Error':
87 'pretty-error > header > colon': {
88 // we hide that too:
89 display: 'none'
90 },
91
92 // our error message
93 'pretty-error > header > message': {
94 // let's change its color:
95 color: 'bright-white',
96
97 // we can use black, red, green, yellow, blue, magenta, cyan, white,
98 // grey, bright-red, bright-green, bright-yellow, bright-blue,
99 // bright-magenta, bright-cyan, and bright-white
100
101 // we can also change the background color:
102 background: 'cyan',
103
104 // it understands paddings too!
105 padding: '0 1' // top/bottom left/right
106 },
107
108 // each trace item ...
109 'pretty-error > trace > item': {
110 // ... can have a margin ...
111 marginLeft: 2,
112
113 // ... and a bullet character!
114 bullet: '"<grey>o</grey>"'
115
116 // Notes on bullets:
117 //
118 // The string inside the quotation mark gets used as the character
119 // to show for the bullet point.
120 //
121 // You can set its color/background color using tags.
122 //
123 // This example sets the background color to white, and the text color
124 // to cyan, the character will be a hyphen with a space character
125 // on each side:
126 // example: '"<bg-white><cyan> - </cyan></bg-white>"'
127 //
128 // Note that we should use a margin of 3, since the bullet will be
129 // 3 characters long.
130 },
131
132 'pretty-error > trace > item > header > pointer > file': {
133 color: 'bright-cyan'
134 },
135
136 'pretty-error > trace > item > header > pointer > colon': {
137 color: 'cyan'
138 },
139
140 'pretty-error > trace > item > header > pointer > line': {
141 color: 'bright-cyan'
142 },
143
144 'pretty-error > trace > item > header > what': {
145 color: 'bright-white'
146 },
147
148 'pretty-error > trace > item > footer > addr': {
149 display: 'none'
150 }
151});
152```
153
154This is how our minimal theme will look like: ![screenshot of our custom theme](https://github.com/AriaMinaei/pretty-error/raw/master/docs/images/custom-theme-screenshot.png)
155
156Read [RenderKid](https://github.com/AriaMinaei/renderkid)'s docs to learn about all the css rules that are supported.
157
158## Customization
159
160There are a few methods to help you customize the contents of your error logs.
161
162Let's instantiate first:
163
164```javascript
165PrettyError = require('pretty-error');
166pe = new PrettyError();
167
168// or:
169pe = require('pretty-error').start();
170```
171
172#### Shortening paths
173
174You might want to substitute long paths with shorter, more readable aliases:
175
176```javascript
177pe.alias('E:/open-source/theatrejs/lib', '(Theatre.js)');
178```
179
180#### Skipping packages
181
182You might want to skip trace lines that belong to specific packages (chai, when, socket.io):
183
184```javascript
185pe.skipPackage('chai', 'when', 'socket.io');
186```
187
188#### Skipping node files
189
190```javascript
191// this will skip node.js, path.js, event.js, etc.
192pe.skipNodeFiles();
193```
194
195#### Skipping paths
196
197```javascript
198pe.skipPath('/home/dir/someFile.js');
199```
200
201#### Skipping by callback
202
203You can customize which trace lines get logged and which won't:
204```javascript
205pe.skip(function(traceLine, lineNumber){
206 // if we know which package this trace line comes from, and it isn't
207 // our 'demo' package ...
208 if (typeof traceLine.packageName !== 'undefined' && traceLine.packageName !== 'demo') {
209 // then skip this line
210 return true;
211 }
212
213 // You can console.log(traceLine) to see all of it's properties.
214 // Don't expect all these properties to be present, and don't assume
215 // that our traceLine is always an object.
216});
217```
218
219#### Modifying each trace line's contents
220
221```javascript
222pe.filter(function(traceLine, lineNumber){
223 // the 'what' clause is something like:
224 // 'DynamicTimeline.module.exports.DynamicTimeline._verifyProp'
225 if (typeof traceLine.what !== 'undefined'){
226
227 // we can shorten it with a regex:
228 traceLine.what = traceLine.what.replace(
229 /(.*\.module\.exports\.)(.*)/, '$2'
230 );
231 }
232});
233```
234
235## Disabling colors
236```javascript
237pe.withoutColors(); // Errors will be rendered without coloring
238```
239
240## Integrating with frameworks
241
242PrettyError is very simple to set up, so it should be easy to use within other frameworks.
243
244### Integrating with [express](https://github.com/visionmedia/express)
245
246Most 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:
247
248```javascript
249// this is app.js
250
251var express = require('express');
252var PrettyError = require('pretty-error');
253
254var app = express();
255
256app.get('/', function(req, res) {
257 // this will throw an error:
258 var a = b;
259});
260
261var server = app.listen(3000, function(){
262 console.log('Server started \n');
263});
264
265
266// we can now instantiaite Prettyerror:
267pe = new PrettyError();
268
269// and use it for our app's error handler:
270app.use(function(err, req, res, next){
271 console.log(pe.render(err));
272 next();
273});
274
275// we can optionally configure prettyError to simplify the stack trace:
276
277pe.skipNodeFiles(); // this will skip events.js and http.js and similar core node files
278pe.skipPackage('express'); // this will skip all the trace lines about express` core and sub-modules
279```
280
281## Troubleshooting
282
283`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.
284
285In order to avoid this problem, it's better to not use `PrettyError.start()` and instead, manually catch errors and render them with PrettyError:
286
287```javascript
288var PrettyError = require('pretty-error');
289var pe = new PrettyError();
290
291// To render exceptions thrown in non-promies code:
292process.on('uncaughtException', function(error){
293 console.log(pe.render(error));
294});
295
296// To render unhandled rejections created in BlueBird:
297process.on('unhandledRejection', function(reason){
298 console.log("Unhandled rejection");
299 console.log(pe.render(reason));
300});
301
302// While PrettyError.start() works out of the box with when.js` unhandled rejections,
303// now that wer'e manually rendering errors, we have to instead use npmjs.org/packages/pretty-monitor
304// to handle when.js rejections.
305
306```
307
308The 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:
309
310```javascript
311// (continued form above)
312
313throw new Error(); // not prettified
314process.nextTick(function(){
315 throw new Error(); // prettified
316});
317
318```
319
320## License
321
322MIT
Note: See TracBrowser for help on using the repository browser.