-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
86 lines (75 loc) · 2.43 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
/*!
* clean-stacktrace <https://github.com/tunnckoCore/clean-stacktrace>
*
* Copyright (c) Charlike Mike Reagent <@tunnckoCore> (https://i.am.charlike.online)
* Released under the MIT license.
*/
'use strict'
var regex = /(?:(?:(?:node|(?:internal\/|.*node_modules\/babel-polyfill\/.*)\w+)\.js:\d+:\d+)|native)/
var nodeInternals = require('stack-utils-node-internals')
/**
* > Removes mostly not needed internal Nodejs entries.
* If you pass `mapper` function, you can make more
* changes to each line of the stack - for example making
* the paths to be relative, not absolute.
*
* **Example**
*
* ```js
* var cleanStack = require('clean-stacktrace')
* var error = new Error('Missing unicorn')
*
* console.log(error.stack)
* // =>
* // Error: Missing unicorn
* // at Object.<anonymous> (/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15)
* // at Module._compile (module.js:409:26)
* // at Object.Module._extensions..js (module.js:416:10)
* // at Module.load (module.js:343:32)
* // at Function.Module._load (module.js:300:12)
* // at Function.Module.runMain (module.js:441:10)
* // at startup (node.js:139:18)
*
* console.log(cleanStack(error.stack))
* // =>
* // Error: Missing unicorn
* // at Object.<anonymous> (/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15)
*
* // or making paths relative
* var path = require('path')
* var stack = clean(error.stack, (line) => {
* var m = /.*\((.*)\).?/.exec(line) || []
* return m[1] ? line.replace(m[1], path.relative(process.cwd(), m[1])) : line
* })
* // =>
* // Error: Missing unicorn
* // at Object.<anonymous> (unicorn.js:2:15)
* ```
*
* @param {String} `stack` an error stack trace
* @param {Function} `mapper` more customization for each line
* @return {String} modified and cleaned stack
* @api public
*/
module.exports = function cleanStacktrace (stack, mapper) {
if (!Array.isArray(stack)) {
stack = stack.split('\n')
}
var result = []
var internals = nodeInternals().concat(regex)
if (!(/^\s*at /.test(stack[0])) && (/^\s*at /i.test(stack[1]))) {
result.push(stack[0])
stack = stack.slice(1)
}
stack.forEach(function (line) {
var isInternal = internals.some(function (internal) {
return internal.test(line)
})
if (isInternal) {
return null
}
result.push(line)
})
result = typeof mapper === 'function' ? result.map(mapper) : result
return result.join('\n')
}