Skip to content

Commit

Permalink
core: refactor handling unhandled rejections
Browse files Browse the repository at this point in the history
Avoid flooding the output since we terminate the interpreter anyway.
  • Loading branch information
saghul committed Nov 7, 2023
1 parent 2c251a0 commit dfda088
Show file tree
Hide file tree
Showing 8 changed files with 2,170 additions and 2,183 deletions.
4,321 changes: 2,157 additions & 2,164 deletions src/bundles/c/core/run-main.c

Large diffs are not rendered by default.

8 changes: 1 addition & 7 deletions src/js/run-main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,7 @@ if (options.help) {
tjs.exit(1);
});
} else {
try {
internals.core.evalFile(filename);
} catch (e) {
console.error(e);

tjs.exit(1);
}
internals.core.evalFile(filename);
}
} else if (command === 'test') {
const [ dir ] = subargv;
Expand Down
3 changes: 1 addition & 2 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ void tjs_addr2obj(JSContext *ctx, JSValue obj, const struct sockaddr *sa) {
static void tjs_dump_obj(JSContext *ctx, FILE *f, JSValueConst val) {
const char *str = JS_ToCString(ctx, val);
if (str) {
const char *prefix = JS_IsException(val) || JS_IsError(ctx, val) ? "Uncaught " : "";
fprintf(f, "%s%s\n", prefix, str);
fprintf(f, "%s\n", str);
JS_FreeCString(ctx, str);
} else {
fprintf(f, "[exception]\n");
Expand Down
7 changes: 4 additions & 3 deletions src/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,11 @@ static void tjs__promise_rejection_tracker(JSContext *ctx,

if (JS_IsException(ret)) {
tjs_dump_error(ctx);
goto fail;
} else {
fprintf(stderr, "Unhandled promise rejection: ");
tjs_dump_error1(ctx, reason);

if (JS_ToBool(ctx, ret)) {
// The event wasn't cancelled, maybe abort.
fail:;
TJSRuntime *qrt = TJS_GetRuntime(ctx);
CHECK_NOT_NULL(qrt);
JS_Throw(qrt->ctx, JS_DupValue(qrt->ctx, reason));
Expand Down Expand Up @@ -239,6 +238,8 @@ TJSRuntime *TJS_NewRuntimeInternal(bool is_worker, TJSRunOptions *options) {
}

void TJS_FreeRuntime(TJSRuntime *qrt) {
JS_RunGC(qrt->rt);

/* Close all loop handles. */
uv_close((uv_handle_t *) &qrt->jobs.prepare, NULL);
uv_close((uv_handle_t *) &qrt->jobs.idle, NULL);
Expand Down
4 changes: 2 additions & 2 deletions tests/test-abort-on-unhandled-rejection.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ const buf = new Uint8Array(4096);
const nread = await proc.stderr.read(buf);
const stderrStr = new TextDecoder().decode(buf.subarray(0, nread));
const status = await proc.wait();
assert.ok(stderrStr.match(/Unhandled promise rejection/) !== null, 'dumps to stderr');
assert.ok(status.exit_status === 1 && status.term_signal === null, 'succeeded');
assert.ok(stderrStr.match(/Error: oops!/) !== null, 'dumps to stderr');
assert.ok(status.exit_status !== 0 && status.term_signal === null, 'succeeded');
2 changes: 1 addition & 1 deletion tests/test-badjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ for (let i = 0; i < NUM_TRIES; i++) {
const proc = tjs.spawn(args);
const status = await proc.wait();

assert.ok(status.exit_status === 1 && status.term_signal === null, 'succeeded')
assert.ok(status.exit_status !== 0 && status.term_signal === null, 'succeeded')
}
2 changes: 1 addition & 1 deletion tests/test-import-http-timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ const args = [
const proc = tjs.spawn(args);
const status = await proc.wait();

assert.ok(status.exit_status === 1 && status.term_signal === null);
assert.ok(status.exit_status !== 0 && status.term_signal === null);
6 changes: 3 additions & 3 deletions tests/test-syntax-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ const args = [
'run',
path.join(import.meta.dirname, 'helpers', 'syntax-error.js')
];
const proc = tjs.spawn(args, { stdout: 'pipe', stderr: 'ignore' });
const proc = tjs.spawn(args, { stdout: 'ignore', stderr: 'pipe' });
const buf = new Uint8Array(4096);
const nread = await proc.stdout.read(buf);
const nread = await proc.stderr.read(buf);
const stderrStr = new TextDecoder().decode(buf.subarray(0, nread));
const status = await proc.wait();
assert.ok(stderrStr.match(/SyntaxError/) !== null, 'dumps to stderr');
assert.ok(status.exit_status === 1 && status.term_signal === null, 'succeeded');
assert.ok(status.exit_status !== 0 && status.term_signal === null, 'succeeded');

0 comments on commit dfda088

Please sign in to comment.