-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
92 lines (81 loc) · 2.19 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* handle-errors <https://github.com/tunnckoCore/handle-errors>
*
* Copyright (c) 2014-2015 Charlike Mike Reagent, contributors.
* Released under the MIT license.
*/
'use strict';
var path = require('path');
var fmt = require('util').format;
/**
* > Useful when you have [hybrid api][hybridify] like [gitclone][gitclone].
* If you work with promises then you will want to throw the errors,
* when callback is in use will handle it in 1st argument.
*
* **Example:**
*
* ```js
* var handleErrors = require('handle-errors')('my-pkg');
*
* handleErrors.error('some err message here');
* //=> throws 'Error: [my-pkg] some error message here'
*
* function _cb(err) {
* // err instanceof Error
* console.log(err.toString());
* //=> 'Error: [my-pkg] some error message here'
*
* console.log(err.shortStack);
* //=> undefined
* }
*
* handleErrors.error('some err message here', _cb);
* ```
*
* @name handleErrors
* @param {String} `label` some marker (package name?)
* @param {Boolean} `stack` when `true` adds `.shortStack` property to the error object
* @return {Error|TypeError} throws it or return `callback` function
* @api public
*/
module.exports = function handleErrors(label, stack) {
checkType(label, 'label');
return {
error: newError,
type: newTypeError
};
function newError(err, cb) {
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) {
checkType(err, 'msg');
err = new TypeError(fmt('[%s] %s', label, err));
err = stack ? buildShortStack(err) : err;
if (!cb) {
throw err;
}
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')
}
}