pax_global_header00006660000000000000000000000064132103267700014513gustar00rootroot0000000000000052 comment=e259af1771d9897d4ce8c0287579b53eb0f70eed pruddy-error-2.0.2/000077500000000000000000000000001321032677000141525ustar00rootroot00000000000000pruddy-error-2.0.2/.gitignore000066400000000000000000000000371321032677000161420ustar00rootroot00000000000000node_modules package-lock.json pruddy-error-2.0.2/LICENSE000066400000000000000000000030121321032677000151530ustar00rootroot00000000000000Copyright (c) 1998, Regents of the University of California All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the University of California, Berkeley nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. pruddy-error-2.0.2/README.md000066400000000000000000000011251321032677000154300ustar00rootroot00000000000000``` This is a clone of the `prettify-error` module which was unpublished by the author. All dependencies have been inlined to prevent future conflicts. ``` ## pruddy-error Prettify given error objects for console outputs ![](https://i.cloudup.com/Vt6PAM3yDA.png) ## Install ``` $ npm install pruddy-error ``` ## Usage ```js var pruddy = require('pruddy-error'); var error = new Error('lorem ipsum'); console.error(pruddy(error) || error); ``` If you'd like to skip some lines from the stack: ```js pruddy(error, { shift: 2 // Will start reading the stack from the third line. }) ``` pruddy-error-2.0.2/ansi-codes.json000066400000000000000000000024071321032677000170750ustar00rootroot00000000000000{ "reset": "\u001b[0m", "bold": "\u001b[1m", "italic": "\u001b[3m", "blink": "\u001b[5m", "underline": "\u001b[4m", "underlineOff": "\u001b[24m", "inverse": "\u001b[7m", "inverseOff": "\u001b[27m", "strikethrough": "\u001b[9m", "strikethroughOff": "\u001b[29m", "def": "\u001b[39m", "white": "\u001b[37m", "black": "\u001b[30m", "grey": "\u001b[90m", "red": "\u001b[31m", "green": "\u001b[32m", "blue": "\u001b[34m", "yellow": "\u001b[33m", "magenta": "\u001b[35m", "cyan": "\u001b[36m", "defBg": "\u001b[49m", "whiteBg": "\u001b[47m", "blackBg": "\u001b[40m", "redBg": "\u001b[41m", "greenBg": "\u001b[42m", "blueBg": "\u001b[44m", "yellowBg": "\u001b[43m", "magentaBg": "\u001b[45m", "cyanBg": "\u001b[46m", "brightBlack": "\u001b[90m", "brightRed": "\u001b[91m", "brightGreen": "\u001b[92m", "brightYellow": "\u001b[93m", "brightBlue": "\u001b[94m", "brightMagenta": "\u001b[95m", "brightCyan": "\u001b[96m", "brightWhite": "\u001b[97m", "brightBlackBg": "\u001b[100m", "brightRedBg": "\u001b[101m", "brightGreenBg": "\u001b[102m", "brightYellowBg": "\u001b[103m", "brightBlueBg": "\u001b[104m", "brightMagentaBg": "\u001b[105m", "brightCyanBg": "\u001b[106m", "brightWhiteBg": "\u001b[107m" } pruddy-error-2.0.2/failing-code.js000066400000000000000000000023701321032677000170330ustar00rootroot00000000000000var failingLine = require('./failing-line'); var nodeRequire; var fs; // // wut, yeah, this is for browserify to prevent it from bundling // a file system polyfill. // if (require('is-node')) { nodeRequire = require; fs = nodeRequire('fs'); nodeRequire = null; } /** * Failing code. * * @param {Error} error Error. * @param {Object} options Configuration. * @returns {Undefined|Array} Parsed failed code. * @private */ function failingCode(error, options) { var ln = failingLine(error, options.shift); if (!ln) return; var doc = options.read && options.read(ln); if (!doc && fs) { try { doc = fs.readFileSync(ln.filename).toString(); } catch (e) { return undefined; } } if (!doc) return undefined; var lines = typeof doc === 'string' ? doc.split('\n') : doc; var result = []; var i = ln.line - 3; while (++i < ln.line + 1) { if (i + 1 != ln.line) { result.push({ line: ln.line - (ln.line - i -1), code: lines[i] }); continue; } result.push({ line: ln.line, col: ln.col, fn: ln.fn, filename: ln.filename, code: lines[i], failed: true }); } return result; } // // Expose the module. // module.exports = failingCode; pruddy-error-2.0.2/failing-line.js000066400000000000000000000050671321032677000170560ustar00rootroot00000000000000/** * Parse v8 stack traces. * * @param {Error} error JavaScript Object * @param {Number} shift Stacks to shift. * @returns {Object} Parsed stack trace. * @private */ function safari(error, shift) { var index = 0; if (shift) index += shift; var fn, filename, line, col; var lines = error.stack.split('\n'); var stack = lines[index] || lines[0]; var fields = stack.split(/\:(\d+)\:(\d+)$/); var numbers = fields.slice(1, 3); fields = fields[0].split('@'); return { fn: fields[0], filename: fields[1], line: Number(numbers[0]), col: Number(numbers[1]) }; } /** * Parse v8 stack traces. * * @param {Error} error JavaScript Object * @param {Number} shift Stacks to shift. * @returns {Object} Parsed stack trace. * @private */ function v8(error, shift) { if (!error || !error.stack) return; var index = 1; if (shift) index += shift; var fn, filename, line, col; var lines = error.stack.split('\n'); var stack = lines[index] || lines[1]; if (!stack) return; var match = stack.match(/at ([\(\)\w\.<>\[\]\s]+) \((.+):(\d+):(\d+)/); if (!match) { match = stack.match(/at (.+):(\d+):(\d+)/); if (!match) return undefined; filename = match[1]; line = Number(match[2]); col = Number(match[3]); } else { fn = match[1]; filename = match[2]; line = Number(match[3]); col = Number(match[4]); } return { fn: fn, filename: filename, line: line, col: col }; } /** * Parse firefox stack traces. * * @param {Error} error JavaScript Object * @param {Number} shift Stacks to shift. * @returns {Object} Parsed stack trace. * @private */ function firefox(error, shift) { var index = 0; if (shift) index += shift; var fn, filename, line, col; var lines = error.stack.split('\n'); var stack = lines[index] || lines[0]; var fields = stack.split(/\:(\d+)$/); var numbers = fields.slice(1, 2); fields = fields[0].split('@'); if (index == 0) { col = error.columnNumber; } return { fn: fields[0], filename: fields[1], line: Number(numbers[0]), col: col }; } /** * Parse the stacktrace and fine the failing line. * * @param {Error} error JavaScript Object * @param {Number} shift Stacks to shift. * @returns {Object} Parsed stack trace. * @public */ function failingLine(error, shift) { if (!error || !error.stack) return; if (/ at /.test(error.stack)) return v8(error, shift); if (/:\d+:\d+$/.test(error.stack)) return safari(error, shift); return firefox(error, shift); } // // Expose the module. // module.exports = failingLine; pruddy-error-2.0.2/format-text.js000066400000000000000000000021011321032677000167540ustar00rootroot00000000000000 /** * Format the text according to the template. * * @param {String} text Template that needs to be formatted. * @param {Arguments..} .. The arguments that need to be applied. * @returns {String} The replaced text. */ function format(text) { var context; if (typeof arguments[1] == 'object' && arguments[1]) { context = arguments[1]; } else { context = Array.prototype.slice.call(arguments, 1); } return String(text).replace(/\{?\{([^{}]+)}}?/g, replace(context)); }; /** * Replaces the placeholders with the actual data. * * @param {object} context data for the template * @returns {String} The new template data * @private */ function replace(context){ return function replacer(tag, name) { if (tag.substring(0, 2) == '{{' && tag.substring(tag.length - 2) == '}}') { return '{' + name + '}'; } if (!context.hasOwnProperty(name)) { return tag; } if (typeof context[name] == 'function') { return context[name](); } return context[name]; } } // // Expose the actual module. // module.exports = format; pruddy-error-2.0.2/index.js000066400000000000000000000045261321032677000156260ustar00rootroot00000000000000var failingCode = require('./failing-code'); var style = require('./style-format'); var format = require('./format-text'); var leftpad = require('left-pad'); /** * The template that is rendered with the error detailed error information. * * @type {String} * @private */ var template = style([ '{bold}{red}{title} {grey}{filename}{reset}', ' {red}{v}', ' {grey}{previousLineNo}. {previousLine}', ' {reset}{failingLineNo}. {failingLine}', ' {grey}{nextLineNo}. {nextLine}', ' {red}{^}{reset}', ' {stack}', '{reset}' ].join('\n')); /** * Replaces the newlines with tabbed newlines. * * @param {String} Stack error stack that needs to be tabbed * @returns {String} Tabbed stacktrace * @private */ function tabStack(stack) { return stack.replace(/\n/g, '\n '); } /** * Shows the column * * @param {Array} code The failing code information. * @param {Number} tabn Tabs * @param {String} ch Character that needs to be shown. * @private */ function showColumn(code, tabn, ch) { var i = String(code[1].line).length + code[1].col + 1 + tabn; var result = ''; while (i--) { result += ' '; } return result + ch; } /** * Reformat an error so it shows detailed debug information. * * @param {Error} error The error that needs to be pretty. * @param {Object} options Addition configuration. * @returns {Undefined|String} Rendered template or bust. * @public */ function pruddy(error, options) { if (!error || !error.stack) return; options = options || {}; var code = options.code || failingCode(error, options); if (!code) return; var previous = String(code[0].line); var failing = String(code[1].line); var next = String(code[2].line); var line = Math.max(previous.length, failing.length, next.length); return format(template, { title: error.message, filename: code[1].filename, previousLine: code[0].code, previousLineNo: leftpad(previous, line), previousColNo: code[0].col, failingLine: code[1].code, failingLineNo: leftpad(failing, line), failingColNo: code[1].col, nextLine: code[2].code, nextLineNo: leftpad(next, line), nextColNo: code[2].col, stack: tabStack(error.stack), '^': showColumn(code, line - failing.length, '^'), 'v': showColumn(code, line - failing.length, 'v') }); } // // Expose the module // module.exports = pruddy; pruddy-error-2.0.2/package.json000066400000000000000000000007601321032677000164430ustar00rootroot00000000000000{ "name": "pruddy-error", "version": "2.0.2", "description": "Prettify given error object", "main": "index.js", "scripts": { "test": "mocha test.js" }, "keywords": [ "error", "pretty", "prettify" ], "repository": { "url": "git@github.com:bigpipe/pruddy-error.git", "type": "git" }, "license": "BSD", "dependencies": { "is-node": "^1.0.2", "left-pad": "^1.2.0" }, "devDependencies": { "assume": "^1.5.2", "mocha": "^4.0.1" } } pruddy-error-2.0.2/style-format.js000066400000000000000000000005071321032677000171400ustar00rootroot00000000000000var ansi = require('./ansi-codes.json'); var format = require('./format-text'); /** * Introduce ASCII as template tags. * * @param {String} text * @returns {String} ANSI injected Template * @public */ function styleFormat(text) { return format(text, ansi); } // // Expose the module. // module.exports = styleFormat; pruddy-error-2.0.2/test.js000066400000000000000000000044031321032677000154700ustar00rootroot00000000000000var assume = require('assume'); var pruddy = require('./'); describe('pruddy', function () { const fixture = new Error('Splode'); it('is exposed as function', function () { assume(pruddy).is.a('function'); }); it('returns undefined if noting is passed', function () { assume(pruddy()).is.a('undefined'); }); it('returns a string when a valid error is given', function () { assume(pruddy(fixture)).is.a('string'); }); it('provides context about the error', function () { const rendered = pruddy(fixture); assume(rendered).contains('v'); assume(rendered).contains(`4. describe('pruddy', function () {`); assume(rendered).contains(`5. const fixture = new Error('Splode');`); assume(rendered).contains(`6.`); assume(rendered).contains('^'); }); describe('shift', function () { it('can shift the stacktrace', function () { const rendered = pruddy(fixture, { shift: 1 }); assume(rendered).contains('v'); assume(rendered).does.not.contains(`4. describe('pruddy', function () {`); assume(rendered).does.not.contains(`5. const fixture = new Error('Splode');`); assume(rendered).does.not.contains(`6.`); assume(rendered).contains('^'); }); }); describe('read', function () { it('can provide a custom document fetcher', function (next) { pruddy(fixture, { read: function read(data) { assume(data).is.a('object'); assume(data.filename).contains('pruddy-error/test.js'); assume(data.line).equals(5); assume(data.col).equals(19); next(); } }); }); it('uses the returned file as source for highlight', function () { const rendered = pruddy(fixture, { read: function read() { return [ 'line 1', 'line 2', 'line 3', 'line 4', 'line 5 with some extra long content for me bois', 'line 6', ].join('\n'); } }); assume(rendered).contains('v'); assume(rendered).contains(`4. line 4`); assume(rendered).contains(`5. line 5 with some extra long content for me bois`); assume(rendered).contains(`6. line 6`); assume(rendered).contains('^'); }); }); });