-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
126 lines (109 loc) · 3.89 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*!
* find-callsite <https://github.com/tunnckoCore/find-callsite>
*
* Copyright (c) Charlike Mike Reagent <@tunnckoCore> (https://i.am.charlike.online)
* Released under the MIT license.
*/
'use strict'
var path = require('path')
var extend = require('extend-shallow')
var relativePaths = require('clean-stacktrace-relative-paths')
/**
* > Find correct callsite where error is started. All that stuff
* is because you not always need the first line of the stack
* to understand where and what happened.
*
* In below example we use `rimraf.sync` to throw some error. That's
* the case when we need to be informed where is the `rimraf.sync` call
* not where it throws. In that case it is on line 6, column 12.
*
* **Example**
*
* ```js
* var findCallsite = require('find-callsite')
* var rimraf = require('rimraf')
*
* function fixture () {
* try {
* rimraf.sync(5555)
* } catch (err) {
* var callsiteLine = findCallsite(err)
* console.log(callsiteLine)
* // => 'at fixture (/home/charlike/apps/find-callsite/example.js:6:12)'
*
* var relativeCallsiteLine = findCallsite(err, {
* relativePaths: true
* })
* console.log(relativeCallsiteLine)
* // => 'at fixture (example.js:6:12)'
* }
* }
*
* fixture()
* ```
*
* @param {Error|Object|String} `error` plain or Error object with stack property, or string stack
* @param {Object} `opts` optional options object
* @param {String} `opts.cwd` give current working directory, default to `process.cwd()`
* @param {Boolean} `opts.relativePaths` make path relative to `opts.cwd`, default `false`
* @return {String} single callsite from whole stack trace, e.g. `at foo (/home/bar/baz.js:33:4)`
* @api public
*/
module.exports = function findCallsite (error, opts) {
if (isString(error)) {
error = { stack: error }
}
if (!isObject(error)) {
throw new TypeError('find-callsite: expect `error` to be an object or string')
}
if (!isString(error.stack)) {
throw new TypeError('find-callsite: expect `error.stack` to be non empty string')
}
opts = extend({ cwd: process.cwd(), relativePaths: false }, opts)
// filepath of very parent
var filepath = cameFrom(module.parent)
// allow making path relative + ensurance
filepath = opts.relativePaths
? path.relative(opts.cwd, filepath)
: filepath
// get the index where it's found in stack
var foundIndex = error.stack.indexOf(String(filepath))
// get whole stack from the begining to that index
var toFoundPlace = error.stack.slice(0, foundIndex)
// get the index of the last new line from that part of the stack
var lastNewLine = toFoundPlace.lastIndexOf('\n')
// get the rest of the stack, from the found index
// to the very end of the stack
var pathPlusRest = error.stack.slice(foundIndex)
// get the index of first new line in that rest part of the stack
var restFirstNewLine = pathPlusRest.indexOf('\n')
// if stack is too short, we will get -1
// so we should use as index the length of the stack
restFirstNewLine = restFirstNewLine === -1
? error.stack.length
: restFirstNewLine
// if both indices are equal,
// then we should use index of the first new line
// otherwise we should use index of the end of the stack
restFirstNewLine = restFirstNewLine === error.stack.length
? restFirstNewLine
: restFirstNewLine + toFoundPlace.length
// we cut exactly the callsite that we need and we trim it
var callsiteLine = error.stack.slice(lastNewLine, restFirstNewLine).trim()
return opts.relativePaths
? relativePaths(opts.cwd)(callsiteLine)
: callsiteLine
}
function isObject (val) {
return val && typeof val === 'object' && !Array.isArray(val)
}
function isString (val) {
return val && typeof val === 'string' && val.length > 0
}
function cameFrom (mod) {
/* istanbul ignore next */
if (mod && mod.parent) {
return cameFrom(mod.parent)
}
return (mod && mod.filename)
}