Skip to content

Commit 3152b7c

Browse files
committed
src: assign ERR_SCRIPT_EXECUTION_* codes in C++
Also modifies the error messages so they include more information and are more consistent. - The message of ERR_SCRIPT_EXECUTION_INTERRUPTED now mentions SIGINT and the trailing period is dropped for consistency. - Added ERR_SCRIPT_EXECUTION_TIMEOUT and include the timeout in the message. PR-URL: #20147 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 94e0e2c commit 3152b7c

12 files changed

+83
-39
lines changed

doc/api/errors.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,10 @@ An attempt was made to `require()` an [ES6 module][].
13711371
Script execution was interrupted by `SIGINT` (For example, when Ctrl+C was
13721372
pressed).
13731373

1374+
### ERR_SCRIPT_EXECUTION_TIMEOUT
1375+
1376+
Script execution timed out, possibly due to bugs in the script being executed.
1377+
13741378
<a id="ERR_SERVER_ALREADY_LISTEN"></a>
13751379
### ERR_SERVER_ALREADY_LISTEN
13761380

lib/internal/errors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,7 @@ E('ERR_NO_LONGER_SUPPORTED', '%s is no longer supported', Error);
952952
E('ERR_OUT_OF_RANGE', outOfRange, RangeError);
953953
E('ERR_REQUIRE_ESM', 'Must use import to load ES Module: %s', Error);
954954
E('ERR_SCRIPT_EXECUTION_INTERRUPTED',
955-
'Script execution was interrupted by `SIGINT`.', Error);
955+
'Script execution was interrupted by `SIGINT`', Error);
956956
E('ERR_SERVER_ALREADY_LISTEN',
957957
'Listen method has been called more than once without closing.', Error);
958958
E('ERR_SERVER_NOT_RUNNING', 'Server is not running.', Error);

src/module_wrap.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,9 @@ void ModuleWrap::Evaluate(const FunctionCallbackInfo<Value>& args) {
286286
// which this timeout is nested, so check whether one of the watchdogs
287287
// from this invocation is responsible for termination.
288288
if (timed_out) {
289-
env->ThrowError("Script execution timed out.");
289+
THROW_ERR_SCRIPT_EXECUTION_TIMEOUT(env, timeout);
290290
} else if (received_signal) {
291-
env->ThrowError("Script execution interrupted.");
291+
THROW_ERR_SCRIPT_EXECUTION_INTERRUPTED(env);
292292
}
293293
}
294294

src/node_contextify.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22+
#include "node_errors.h"
2223
#include "node_internals.h"
2324
#include "node_watchdog.h"
2425
#include "base_object-inl.h"
@@ -858,9 +859,9 @@ class ContextifyScript : public BaseObject {
858859
// which this timeout is nested, so check whether one of the watchdogs
859860
// from this invocation is responsible for termination.
860861
if (timed_out) {
861-
env->ThrowError("Script execution timed out.");
862+
node::THROW_ERR_SCRIPT_EXECUTION_TIMEOUT(env, timeout);
862863
} else if (received_signal) {
863-
env->ThrowError("Script execution interrupted.");
864+
node::THROW_ERR_SCRIPT_EXECUTION_INTERRUPTED(env);
864865
}
865866
}
866867

src/node_errors.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
#include "env-inl.h"
99
#include "v8.h"
1010

11+
// Use ostringstream to print exact-width integer types
12+
// because the format specifiers are not available on AIX.
13+
#include <sstream>
14+
1115
namespace node {
1216

1317
// Helpers to construct errors similar to the ones provided by
@@ -24,6 +28,8 @@ namespace node {
2428
V(ERR_MEMORY_ALLOCATION_FAILED, Error) \
2529
V(ERR_MISSING_ARGS, TypeError) \
2630
V(ERR_MISSING_MODULE, Error) \
31+
V(ERR_SCRIPT_EXECUTION_INTERRUPTED, Error) \
32+
V(ERR_SCRIPT_EXECUTION_TIMEOUT, Error) \
2733
V(ERR_STRING_TOO_LONG, Error) \
2834
V(ERR_BUFFER_TOO_LARGE, Error)
2935

@@ -49,7 +55,9 @@ namespace node {
4955

5056
#define PREDEFINED_ERROR_MESSAGES(V) \
5157
V(ERR_INDEX_OUT_OF_RANGE, "Index out of range") \
52-
V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory")
58+
V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory") \
59+
V(ERR_SCRIPT_EXECUTION_INTERRUPTED, \
60+
"Script execution was interrupted by `SIGINT`")
5361

5462
#define V(code, message) \
5563
inline v8::Local<v8::Value> code(v8::Isolate* isolate) { \
@@ -62,6 +70,13 @@ namespace node {
6270
#undef V
6371

6472
// Errors with predefined non-static messages
73+
inline void THROW_ERR_SCRIPT_EXECUTION_TIMEOUT(Environment* env,
74+
int64_t timeout) {
75+
std::ostringstream message;
76+
message << "Script execution timed out after ";
77+
message << timeout << "ms";
78+
THROW_ERR_SCRIPT_EXECUTION_TIMEOUT(env, message.str().c_str());
79+
}
6580

6681
inline v8::Local<v8::Value> ERR_BUFFER_TOO_LARGE(v8::Isolate *isolate) {
6782
char message[128];

test/parallel/test-repl-sigint-nested-eval.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ child.stdout.once('data', common.mustCall(() => {
3434
}));
3535

3636
child.on('close', function(code) {
37-
assert.strictEqual(code, 0);
37+
const expected = 'Script execution was interrupted by `SIGINT`';
3838
assert.ok(
39-
stdout.includes('Script execution interrupted.'),
40-
`Expected stdout to contain "Script execution interrupted.", got ${stdout}`
39+
stdout.includes(expected),
40+
`Expected stdout to contain "${expected}", got ${stdout}`
4141
);
4242
assert.ok(
4343
stdout.includes('foobar'),

test/parallel/test-repl-sigint.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ child.stdout.once('data', common.mustCall(() => {
3535

3636
child.on('close', function(code) {
3737
assert.strictEqual(code, 0);
38+
const expected = 'Script execution was interrupted by `SIGINT`';
3839
assert.ok(
39-
stdout.includes('Script execution interrupted.\n'),
40-
`Expected stdout to contain "Script execution interrupted.", got ${stdout}`
40+
stdout.includes(expected),
41+
`Expected stdout to contain "${expected}", got ${stdout}`
4142
);
4243
assert.ok(
4344
stdout.includes('42042\n'),

test/parallel/test-repl-top-level-await.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ async function ctrlCTest() {
161161
]), [
162162
'await timeout(100000)\r',
163163
'Thrown: Error [ERR_SCRIPT_EXECUTION_INTERRUPTED]: ' +
164-
'Script execution was interrupted by `SIGINT`.',
164+
'Script execution was interrupted by `SIGINT`',
165165
PROMPT
166166
]);
167167
}

test/parallel/test-vm-sigint-existing-handler.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ if (process.argv[2] === 'child') {
3636
[];
3737
const options = { breakOnSigint: true };
3838

39-
assert.throws(() => { vm[method](script, ...args, options); },
40-
/^Error: Script execution interrupted\.$/);
39+
common.expectsError(
40+
() => { vm[method](script, ...args, options); },
41+
{
42+
code: 'ERR_SCRIPT_EXECUTION_INTERRUPTED',
43+
message: 'Script execution was interrupted by `SIGINT`'
44+
});
4145
assert.strictEqual(firstHandlerCalled, 0);
4246
assert.strictEqual(onceHandlerCalled, 0);
4347

test/parallel/test-vm-sigint.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@ if (process.argv[2] === 'child') {
2424
for (let i = 0; i < listeners; i++)
2525
process.on('SIGINT', common.mustNotCall());
2626

27-
assert.throws(() => { vm[method](script, ...args, options); },
28-
/^Error: Script execution interrupted\.$/);
27+
common.expectsError(
28+
() => { vm[method](script, ...args, options); },
29+
{
30+
code: 'ERR_SCRIPT_EXECUTION_INTERRUPTED',
31+
message: 'Script execution was interrupted by `SIGINT`'
32+
});
2933
return;
3034
}
3135

0 commit comments

Comments
 (0)