Skip to content

Commit 03faafb

Browse files
authored
Catch async worker route errors (#33)
1 parent 5188004 commit 03faafb

2 files changed

Lines changed: 21 additions & 8 deletions

File tree

tests/strategy_switch_worker_validation.mjs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { readFileSync } from "node:fs";
33
import { fileURLToPath } from "node:url";
44
import { dirname, resolve } from "node:path";
55

6-
import { __test } from "../web/strategy-switch-console/worker.js";
6+
import worker, { __test } from "../web/strategy-switch-console/worker.js";
77

88
const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
99
const indexHtml = readFileSync(resolve(root, "web/strategy-switch-console/index.html"), "utf8");
@@ -51,6 +51,17 @@ const crossOriginError = captureError(
5151
assert.match(crossOriginError.message, /cross-origin request rejected/);
5252
assert.equal(crossOriginError.status, 403);
5353

54+
const unauthorizedSyncResponse = await worker.fetch(
55+
new Request("https://switch.example/api/internal/sync-account-default", {
56+
method: "POST",
57+
headers: { "Content-Type": "application/json" },
58+
body: "{}",
59+
}),
60+
{ STRATEGY_SWITCH_SYNC_TOKEN: "test-sync-token" },
61+
);
62+
assert.equal(unauthorizedSyncResponse.status, 401);
63+
assert.match((await unauthorizedSyncResponse.json()).error, /internal sync token is invalid/);
64+
5465
assert.equal(
5566
await __test.withTimeout(new Promise((resolve) => setTimeout(() => resolve("late"), 25)), 1, "fallback"),
5667
"fallback",

web/strategy-switch-console/worker.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,23 @@ export default {
4949
async fetch(request, env) {
5050
const url = new URL(request.url);
5151
try {
52-
if (url.pathname === "/login") return startLogin(request, env);
53-
if (url.pathname === "/callback") return finishLogin(request, env);
54-
if (url.pathname === "/admin") return adminPage(request, env);
52+
if (url.pathname === "/login") return await startLogin(request, env);
53+
if (url.pathname === "/callback") return await finishLogin(request, env);
54+
if (url.pathname === "/admin") return await adminPage(request, env);
5555
if (url.pathname === "/api/session") return json(await sessionPayload(request, env));
5656
if (url.pathname === "/api/strategy-profiles") return json(await strategyProfilesPayload(env));
5757
if (url.pathname === "/api/config") return json(await configPayload(request, env));
58-
if (url.pathname === "/api/admin/config" && request.method === "GET") return adminConfigResponse(request, env);
58+
if (url.pathname === "/api/admin/config" && request.method === "GET") {
59+
return await adminConfigResponse(request, env);
60+
}
5961
if (url.pathname === "/api/admin/config" && request.method === "POST") {
60-
return saveAdminConfig(request, env);
62+
return await saveAdminConfig(request, env);
6163
}
6264
if (url.pathname === "/api/internal/sync-account-default" && request.method === "POST") {
63-
return syncAccountDefaultResponse(request, env);
65+
return await syncAccountDefaultResponse(request, env);
6466
}
6567
if (url.pathname === "/api/logout" && request.method === "POST") return logout(request);
66-
if (url.pathname === "/api/switch" && request.method === "POST") return dispatchSwitch(request, env);
68+
if (url.pathname === "/api/switch" && request.method === "POST") return await dispatchSwitch(request, env);
6769
return html(PAGE_HTML);
6870
} catch (error) {
6971
return json({ ok: false, error: error.message || "unexpected error" }, error.status || 500);

0 commit comments

Comments
 (0)