Skip to content

Commit 56a6434

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.
1 parent 67e2a15 commit 56a6434

12 files changed

+81
-39
lines changed

doc/api/errors.md

+4
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

+1-1
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

+2-2
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

+3-2
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

+14-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include "env-inl.h"
99
#include "v8.h"
1010

11+
#include <inttypes.h>
12+
1113
namespace node {
1214

1315
// Helpers to construct errors similar to the ones provided by
@@ -24,6 +26,8 @@ namespace node {
2426
V(ERR_MEMORY_ALLOCATION_FAILED, Error) \
2527
V(ERR_MISSING_ARGS, TypeError) \
2628
V(ERR_MISSING_MODULE, Error) \
29+
V(ERR_SCRIPT_EXECUTION_INTERRUPTED, Error) \
30+
V(ERR_SCRIPT_EXECUTION_TIMEOUT, Error) \
2731
V(ERR_STRING_TOO_LONG, Error) \
2832
V(ERR_BUFFER_TOO_LARGE, Error)
2933

@@ -49,7 +53,9 @@ namespace node {
4953

5054
#define PREDEFINED_ERROR_MESSAGES(V) \
5155
V(ERR_INDEX_OUT_OF_RANGE, "Index out of range") \
52-
V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory")
56+
V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory") \
57+
V(ERR_SCRIPT_EXECUTION_INTERRUPTED, \
58+
"Script execution was interrupted by `SIGINT`")
5359

5460
#define V(code, message) \
5561
inline v8::Local<v8::Value> code(v8::Isolate* isolate) { \
@@ -62,6 +68,13 @@ namespace node {
6268
#undef V
6369

6470
// Errors with predefined non-static messages
71+
inline void THROW_ERR_SCRIPT_EXECUTION_TIMEOUT(Environment* env,
72+
int64_t timeout) {
73+
char message[128];
74+
snprintf(message, sizeof(message),
75+
"Script execution timed out after %" PRId64 "ms", timeout);
76+
THROW_ERR_SCRIPT_EXECUTION_TIMEOUT(env, message);
77+
}
6578

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

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

+3-3
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

+3-2
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

+1-1
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

+6-2
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

+6-2
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

test/parallel/test-vm-timeout.js

+37-22
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,50 @@ const assert = require('assert');
2525
const vm = require('vm');
2626

2727
// Timeout of 100ms executing endless loop
28-
assert.throws(function() {
29-
vm.runInThisContext('while(true) {}', { timeout: 100 });
30-
}, /^Error: Script execution timed out\.$/);
28+
assert.throws(
29+
function() {
30+
vm.runInThisContext('while(true) {}', { timeout: 100 });
31+
},
32+
{
33+
code: 'ERR_SCRIPT_EXECUTION_TIMEOUT',
34+
message: 'Script execution timed out after 100ms'
35+
});
3136

3237
// Timeout of 1000ms, script finishes first
3338
vm.runInThisContext('', { timeout: 1000 });
3439

3540
// Nested vm timeouts, inner timeout propagates out
36-
assert.throws(function() {
37-
const context = {
38-
log: console.log,
39-
runInVM: function(timeout) {
40-
vm.runInNewContext('while(true) {}', context, { timeout });
41-
}
42-
};
43-
vm.runInNewContext('runInVM(10)', context, { timeout: 10000 });
44-
throw new Error('Test 5 failed');
45-
}, /Script execution timed out\./);
41+
assert.throws(
42+
function() {
43+
const context = {
44+
log: console.log,
45+
runInVM: function(timeout) {
46+
vm.runInNewContext('while(true) {}', context, { timeout });
47+
}
48+
};
49+
vm.runInNewContext('runInVM(10)', context, { timeout: 10000 });
50+
throw new Error('Test 5 failed');
51+
},
52+
{
53+
code: 'ERR_SCRIPT_EXECUTION_TIMEOUT',
54+
message: 'Script execution timed out after 10ms'
55+
});
4656

4757
// Nested vm timeouts, outer timeout is shorter and fires first.
48-
assert.throws(function() {
49-
const context = {
50-
runInVM: function(timeout) {
51-
vm.runInNewContext('while(true) {}', context, { timeout });
52-
}
53-
};
54-
vm.runInNewContext('runInVM(10000)', context, { timeout: 100 });
55-
throw new Error('Test 6 failed');
56-
}, /Script execution timed out\./);
58+
assert.throws(
59+
function() {
60+
const context = {
61+
runInVM: function(timeout) {
62+
vm.runInNewContext('while(true) {}', context, { timeout });
63+
}
64+
};
65+
vm.runInNewContext('runInVM(10000)', context, { timeout: 100 });
66+
throw new Error('Test 6 failed');
67+
},
68+
{
69+
code: 'ERR_SCRIPT_EXECUTION_TIMEOUT',
70+
message: 'Script execution timed out after 100ms'
71+
});
5772

5873
// Nested vm timeouts, inner script throws an error.
5974
assert.throws(function() {

test/sequential/test-vm-timeout-rethrow.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ if (process.argv[2] === 'child') {
3939
});
4040

4141
process.on('exit', function() {
42-
assert.ok(/Script execution timed out/.test(err));
42+
assert.ok(/Script execution timed out after 1ms/.test(err));
4343
});
4444
}

0 commit comments

Comments
 (0)