Skip to content

Commit 248808b

Browse files
committed
Improved solution
1 parent 2c94dcc commit 248808b

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

index.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -265,19 +265,25 @@ function log (message, site) {
265265
*/
266266

267267
function callSiteLocation (callSite) {
268-
var file = callSite.getFileName ? (callSite.getFileName() || '<anonymous>') : '<unknown file>'
269-
var line = callSite.getLineNumber ? callSite.getLineNumber() : '<unknown line number>'
270-
var colm = callSite.getColumnNumber ? callSite.getColumnNumber() : '<unknown column number>'
271-
272-
if (callSite.isEval && callSite.isEval()) {
273-
file = callSite.getEvalOrigin() + ', ' + file
268+
var functionName, site
269+
if (callSiteLocation) {
270+
functionName = callSite.getFunctionName()
271+
var file = callSite.getFileName() || '<anonymous>'
272+
var line = callSite.getLineNumber()
273+
var colm = callSite.getColumnNumber()
274+
if (callSite.isEval()) {
275+
file = callSite.getEvalOrigin() + ', ' + file
276+
}
277+
site = [file, line, colm]
278+
} else {
279+
// eslint-disable-next-line no-param-reassign
280+
callSite = {}
281+
callSite.getThis = function () { return null }
282+
functionName = '<unknown function>'
283+
site = ['<unknown file>', '<unknown line>', '<unknown column>']
274284
}
275-
276-
var site = [file, line, colm]
277-
278285
site.callSite = callSite
279-
site.name = callSite.getFunctionName ? callSite.getFunctionName() : '<unknown function>'
280-
286+
site.name = functionName
281287
return site
282288
}
283289

@@ -389,6 +395,12 @@ function getStack () {
389395
// capture the stack
390396
Error.captureStackTrace(obj)
391397

398+
if (typeof obj.stack === 'string' || obj.stack instanceof String) {
399+
// Means that prepareObjectStackTrace failed, obj.stack is not a CallSite array.
400+
// We fallback to returning an empty array.
401+
return []
402+
}
403+
392404
// slice this function off the top
393405
var stack = obj.stack.slice(1)
394406

0 commit comments

Comments
 (0)