diff --git a/src/node_errors.cc b/src/node_errors.cc index 65f95c3157a..609601328f7 100644 --- a/src/node_errors.cc +++ b/src/node_errors.cc @@ -136,8 +136,13 @@ static std::string GetErrorSource(Isolate* isolate, // Print (filename):(line number): (message). ScriptOrigin origin = message->GetScriptOrigin(); - node::Utf8Value filename(isolate, message->GetScriptResourceName()); - const char* filename_string = *filename; + std::string filename_string; + if (message->GetScriptResourceName()->IsUndefined()) { + filename_string = ""; + } else { + node::Utf8Value filename(isolate, message->GetScriptResourceName()); + filename_string = filename.ToString(); + } int linenum = message->GetLineNumber(context).FromJust(); int script_start = (linenum - origin.LineOffset()) == 1 diff --git a/test/fixtures/errors/throw_in_eval_anonymous.js b/test/fixtures/errors/throw_in_eval_anonymous.js new file mode 100644 index 00000000000..aa9ab6a0580 --- /dev/null +++ b/test/fixtures/errors/throw_in_eval_anonymous.js @@ -0,0 +1,9 @@ +'use strict'; +require('../../common'); + +Error.stackTraceLimit = 1; +eval(` + + throw new Error('error in anonymous script'); + +`) diff --git a/test/fixtures/errors/throw_in_eval_anonymous.snapshot b/test/fixtures/errors/throw_in_eval_anonymous.snapshot new file mode 100644 index 00000000000..e6f731f4d99 --- /dev/null +++ b/test/fixtures/errors/throw_in_eval_anonymous.snapshot @@ -0,0 +1,8 @@ +:* + throw new Error('error in anonymous script'); + ^ + +Error: error in anonymous script + at eval (eval at (*throw_in_eval_anonymous.js:*:*), :*:*) + +Node.js * diff --git a/test/fixtures/errors/throw_in_eval_named.js b/test/fixtures/errors/throw_in_eval_named.js new file mode 100644 index 00000000000..0d33fcf4d05 --- /dev/null +++ b/test/fixtures/errors/throw_in_eval_named.js @@ -0,0 +1,9 @@ +'use strict'; +require('../../common'); + +Error.stackTraceLimit = 1; +eval(` + + throw new Error('error in named script'); + +//# sourceURL=evalscript.js`) diff --git a/test/fixtures/errors/throw_in_eval_named.snapshot b/test/fixtures/errors/throw_in_eval_named.snapshot new file mode 100644 index 00000000000..3be4c358473 --- /dev/null +++ b/test/fixtures/errors/throw_in_eval_named.snapshot @@ -0,0 +1,8 @@ +evalscript.js:* + throw new Error('error in named script'); + ^ + +Error: error in named script + at eval (evalscript.js:*:*) + +Node.js * diff --git a/test/parallel/test-node-output-errors.mjs b/test/parallel/test-node-output-errors.mjs index 1f93330834e..3d913475d8d 100644 --- a/test/parallel/test-node-output-errors.mjs +++ b/test/parallel/test-node-output-errors.mjs @@ -63,6 +63,8 @@ describe('errors output', { concurrency: !process.env.TEST_PARALLEL }, () => { { name: 'errors/if-error-has-good-stack.js', transform: errTransform }, { name: 'errors/throw_custom_error.js', transform: errTransform }, { name: 'errors/throw_error_with_getter_throw.js', transform: errTransform }, + { name: 'errors/throw_in_eval_anonymous.js', transform: errTransform }, + { name: 'errors/throw_in_eval_named.js', transform: errTransform }, { name: 'errors/throw_in_line_with_tabs.js', transform: errTransform }, { name: 'errors/throw_non_error.js', transform: errTransform }, { name: 'errors/throw_null.js', transform: errTransform },