Skip to content

Commit

Permalink
add optional 2nd arg for short stack trace
Browse files Browse the repository at this point in the history
  • Loading branch information
tunnckoCore committed Jan 30, 2015
1 parent bc2901a commit b55ee0c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
Empty file removed .bump
Empty file.
31 changes: 27 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,29 @@
var path = require('path');
var fmt = require('util').format;

module.exports = function handleErrors(label) {
module.exports = function handleErrors(label, stack) {
checkType(label, 'label');

return {
error: newError,
type: newTypeError
};

function newError(err, cb) {
err = new Error(fmt('%s: %s', label, err));
checkType(err, 'msg');
err = new Error(fmt('[%s] %s', label, err));
err = stack ? buildShortStack(err) : err;

if (!cb) {
throw err;
}

return cb(err);
}

function newTypeError(err, cb) {
err = new TypeError(fmt('%s: %s', label, err));
checkType(err, 'msg');
err = new TypeError(fmt('[%s] %s', label, err));
err = stack ? buildShortStack(err) : err;

if (!cb) {
throw err;
Expand All @@ -36,3 +41,21 @@ module.exports = function handleErrors(label) {
return cb(err);
}
};

function buildShortStack(err) {
var shortStack = err.stack.split('\n').filter(Boolean).filter(function(line) {
return /\//.test(line)
});
shortStack[0] = err.toString();
err.shortStack = shortStack.join('\n');
return err;
}

function checkType(arg, name) {
if (!arg) {
throw new Error('[handle-errors] should have `' + name + '`')
}
if (typeof arg !== 'string') {
throw new TypeError('[handle-errors] expect `' + name + '` be string')
}
}

0 comments on commit b55ee0c

Please sign in to comment.