Skip to content

Commit

Permalink
src: fix undefined script name in error source
Browse files Browse the repository at this point in the history
PR-URL: #56502
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
legendecas authored Jan 9, 2025
1 parent 6b3937a commit 483e3d5
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 2 deletions.
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

0 comments on commit 483e3d5

Please sign in to comment.