src: fix undefined script name in error source

PR-URL: https://github.com/nodejs/node/pull/56502
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
pull/49292/merge
Chengzhong Wu 2025-01-09 14:06:31 +00:00 committed by GitHub
parent 6b3937a072
commit 483e3d5e4b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 43 additions and 2 deletions

View File

@ -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

View File

@ -0,0 +1,9 @@
'use strict';
require('../../common');
Error.stackTraceLimit = 1;
eval(`
throw new Error('error in anonymous script');
`)

View File

@ -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 *

View File

@ -0,0 +1,9 @@
'use strict';
require('../../common');
Error.stackTraceLimit = 1;
eval(`
throw new Error('error in named script');
//# sourceURL=evalscript.js`)

View File

@ -0,0 +1,8 @@
evalscript.js:*
throw new Error('error in named script');
^
Error: error in named script
at eval (evalscript.js:*:*)
Node.js *

View File

@ -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 },