Permalink
Browse files

initial commit

  • Loading branch information...
azer committed Mar 15, 2014
0 parents commit 3115fcbe9c359e8144f1853488af34ded9ba427b
Showing with 127 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +4 −0 .npmignore
  3. +26 −0 README.md
  4. +10 −0 example.js
  5. +59 −0 index.js
  6. +26 −0 package.json
@@ -0,0 +1,2 @@
+node_modules
+npm-debug.log
@@ -0,0 +1,4 @@
+test
+test.js
+example
+examples
@@ -0,0 +1,26 @@
+## prettify-error
+
+Prettify given error object
+
+![](https://i.cloudup.com/y9mYC3b8t8.png)
+
+## Install
+
+```bash
+$ npm install prettify-error
+```
+
+## Usage
+
+```js
+var prettify = require('prettify-error')
+var err = new Error('lorem ipsum')
+
+console.error(prettify(error))
+```
+
+If you'd like to skip some lines from the stack:
+
+```js
+prettify(error, 2) // Will start reading the stack from the third line.
+```
@@ -0,0 +1,10 @@
+var prettify = require('./');
+
+process.on('uncaughtException', function (error) {
+ console.error(prettify(error));
+});
+
+
+true;
+314156 + err++;
+false;
@@ -0,0 +1,59 @@
+var failingCode = require("failing-code");
+var format = require("format-text");
+var leftpad = require("left-pad");
+var style = require("style-format");
+
+var template = style('{bold}{red}{title} {grey}{filename}{reset}\n'
+ + ' {red}{v}\n'
+ + ' {grey}{previousLineNo}. {previousLine}\n'
+ + ' {reset}{failingLineNo}. {failingLine}\n'
+ + ' {grey}{nextLineNo}. {nextLine}\n'
+ + ' {red}{^}{reset}\n'
+ + ' {stack}\n'
+ + '{reset}');
+
+module.exports = prettifyError;
+
+function prettifyError (error, shift) {
+ if (!error.stack) return console.error(error);
+
+ var code = failingCode(error, undefined, shift);
+ var previousLineNo = String(code[0].line);
+ var failingLineNo = String(code[1].line);
+ var nextLineNo = String(code[2].line);
+ var linumlen = Math.max(previousLineNo.length,
+ failingLineNo.length,
+ nextLineNo.length);
+
+ return format(template, {
+ title: error.message,
+ filename: code[1].filename,
+ previousLine: code[0].code,
+ previousLineNo: leftpad(previousLineNo, linumlen),
+ previousColNo: code[0].col,
+ failingLine: code[1].code,
+ failingLineNo: leftpad(failingLineNo, linumlen),
+ failingColNo: code[1].col,
+ nextLine: code[2].code,
+ nextLineNo: leftpad(nextLineNo, linumlen),
+ nextColNo: code[2].col,
+ stack: tabStack(error.stack),
+ '^': showColumn(code, linumlen - failingLineNo.length, '^'),
+ 'v': showColumn(code, linumlen - failingLineNo.length, 'v')
+ });
+}
+
+function showColumn (code, tabn, ch) {
+ var result = '';
+ var i = String(code[1].line).length + code[1].col + 1 + tabn;
+
+ while (i--) {
+ result += ' ';
+ }
+
+ return result + ch;
+}
+
+function tabStack (stack) {
+ return stack.replace(/\n/g, '\n ');
+}
@@ -0,0 +1,26 @@
+{
+ "name": "prettify-error",
+ "version": "0.0.0",
+ "description": "Prettify given error object",
+ "main": "index.js",
+ "scripts": {
+ "test": "mocha"
+ },
+ "dependencies": {
+ "format-text": "0.x",
+ "failing-code": "0.x",
+ "left-pad": "0.0.0",
+ "style-format": "0.0.0"
+ },
+ "keywords": [
+ "error",
+ "pretty",
+ "prettify"
+ ],
+ "repository": {
+ "url": "git@github.com:azer/prettify-error.git",
+ "type": "git"
+ },
+ "author": "azer",
+ "license": "BSD"
+}

0 comments on commit 3115fcb

Please sign in to comment.