Skip to content

vm: Error.prepareStackTrace behaves inconsistently across versions #21270

Closed
@bengl

Description

  • Version: 8.11.2
  • Platform: Linux 4.16.4-1-ARCH
  • Subsystem: vm

In Node 6.x (tested on v6.14.2), the following results in the prepareStackTrace defined inside the context being used:

const { runInNewContext } = require('vm');
console.log(runInNewContext(`() => {
  Error.prepareStackTrace = () => 'hello';
  return new Error();
}`)().stack);

The stack is logged as hello.

In Node 8.x (tested with 8.11.2), it will just output a normal stack, ignoring the prepareStackTrace defined in the context.

It seems that in Node 8, the prepareStackTrace from the currently running context (i.e. the one where .stack is called) is used, whereas in Node 6, the one where the Error was instantiated is used.

Note that this only happens when the Error is passed along, and not when it's a thrown exception. I suspect that's because when it's thrown, DecorateErrorStack is run, which gets the stack before it's passed to the main context:

node/src/node_contextify.cc

Lines 735 to 747 in faf417b

static void DecorateErrorStack(Environment* env, const TryCatch& try_catch) {
Local<Value> exception = try_catch.Exception();
if (!exception->IsObject())
return;
Local<Object> err_obj = exception.As<Object>();
if (IsExceptionDecorated(env, err_obj))
return;
AppendExceptionLine(env, exception, try_catch.Message(), CONTEXTIFY_ERROR);
Local<Value> stack = err_obj->Get(env->stack_string());

Note also that when the stack is retrieved inside the new context, the in-context prepareStackTrace is always used, regardless of version.

Metadata

Assignees

No one assigned

    Labels

    vmIssues and PRs related to the vm subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions