Still assign error's raw stack when prepareStackTrace is overridden

Signed-off-by: Nathan Sobo <nathan@github.com>
This commit is contained in:
Max Brunsfeld
2015-11-23 15:30:11 -08:00
committed by Nathan Sobo
parent 4616b426b0
commit 4bc46db355
2 changed files with 17 additions and 12 deletions

View File

@@ -71,13 +71,16 @@ describe 'CompileCache', ->
expect(CSONParser.parse.callCount).toBe 1
describe 'overriding Error.prepareStackTrace', ->
it 'removes the override on the next tick', ->
it 'removes the override on the next tick, and always assigns the raw stack', ->
Error.prepareStackTrace = -> 'a-stack-trace'
error = new Error("Oops")
expect(error.stack).toBe 'a-stack-trace'
expect(Array.isArray(error.getRawStack())).toBe true
waits(1)
runs ->
error = new Error("Oops again")
expect(error.stack).not.toBe 'a-stack-trace'
console.log error.stack
expect(error.stack).toContain('compile-cache-spec.coffee')
expect(Array.isArray(error.getRawStack())).toBe true

View File

@@ -158,29 +158,31 @@ require('source-map-support').install({
}
})
var sourceMapPrepareStackTrace = Error.prepareStackTrace
var prepareStackTraceWithSourceMapping = Error.prepareStackTrace
// Enable Grim to access the raw stack by customizing Error.prepareStackTrace
function prepareStackTraceWithRawStack (error, frames) {
let prepareStackTrace = prepareStackTraceWithSourceMapping
function prepareStackTraceWithRawStackAssignment (error, frames) {
error.rawStack = frames
return sourceMapPrepareStackTrace(error, frames)
return prepareStackTrace(error, frames)
}
let prepareStackTrace = prepareStackTraceWithRawStack
// Prevent coffee-script from reassigning Error.prepareStackTrace
Object.defineProperty(Error, 'prepareStackTrace', {
get: function () { return prepareStackTrace },
get: function () {
return prepareStackTraceWithRawStackAssignment
},
set: function (newValue) {
prepareStackTrace = newValue
process.nextTick(function () {
prepareStackTrace = prepareStackTraceWithRawStack
prepareStackTrace = prepareStackTraceWithSourceMapping
})
}
})
Error.prototype.getRawStack = function () { // eslint-disable-line no-extend-native
// Call this.stack first to ensure rawStack is generated
// Access this.stack to ensure prepareStackTrace has been run on this error
// because it assigns this.rawStack as a side-effect
this.stack
return this.rawStack
}