Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: fix undefined script name in error source #56502

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/node_errors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<anonymous_script>";
} else {
node::Utf8Value filename(isolate, message->GetScriptResourceName());
filename_string = filename.ToString();
}
int linenum = message->GetLineNumber(context).FromJust();

int script_start = (linenum - origin.LineOffset()) == 1
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/errors/throw_in_eval_anonymous.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';
require('../../common');

Error.stackTraceLimit = 1;
eval(`

throw new Error('error in anonymous script');

`)
8 changes: 8 additions & 0 deletions test/fixtures/errors/throw_in_eval_anonymous.snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<anonymous_script>:*
throw new Error('error in anonymous script');
^

Error: error in anonymous script
at eval (eval at <anonymous> (*throw_in_eval_anonymous.js:*:*), <anonymous>:*:*)

Node.js *
9 changes: 9 additions & 0 deletions test/fixtures/errors/throw_in_eval_named.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions test/fixtures/errors/throw_in_eval_named.snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
evalscript.js:*
throw new Error('error in named script');
^

Error: error in named script
at eval (evalscript.js:*:*)

Node.js *
2 changes: 2 additions & 0 deletions test/parallel/test-node-output-errors.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
Loading