Skip to content

Commit cdf2a6e

Browse files
committed
fix(worker): adapter preserves transport-failure shapes exactly
1 parent 5e892a1 commit cdf2a6e

2 files changed

Lines changed: 37 additions & 4 deletions

File tree

‎dashboard/github-api.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export const DEFAULT_GITHUB_API_URL = "https://api.github.com";
33
const GITHUB_APP_TIMEOUT_MS = 4500;
44
const GITHUB_RATE_LIMIT_FALLBACK_MS = 15 * 60 * 1000;
55
const GITHUB_RATE_LIMIT_MAX_RETRY_MS = 2 * 60 * 60 * 1000;
6+
const workerTransportErrors = new WeakMap<GitHubRequestError, unknown>();
67

78
type GithubApiEnv = Record<string, unknown>;
89

@@ -141,11 +142,13 @@ export async function githubAppJson(path, appJwt, options: GithubAppJsonOptions
141142
const timedOut =
142143
controller.signal.aborted ||
143144
(error instanceof Error && (error.name === "AbortError" || error.message === "timeout"));
144-
throw new GitHubRequestError(
145+
const requestError = new GitHubRequestError(
145146
`${options.errorLabel || "GitHub App"} ${timedOut ? "timed out" : "network failure"}`,
146147
undefined,
147148
timedOut,
148149
);
150+
if (!controller.signal.aborted) workerTransportErrors.set(requestError, error);
151+
throw requestError;
149152
} finally {
150153
clearTimeout(timeout);
151154
}
@@ -178,7 +181,10 @@ export async function githubAppJsonAsPlainError(
178181
}
179182

180183
function throwPlainGitHubRequestError(error: unknown): never {
181-
if (error instanceof GitHubRequestError) throw new Error(error.message);
184+
if (error instanceof GitHubRequestError) {
185+
if (workerTransportErrors.has(error)) throw workerTransportErrors.get(error);
186+
throw new Error(error.message);
187+
}
182188
throw error;
183189
}
184190

‎test/github-api.test.ts‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,43 @@ test("canonical GitHub App signer converts PKCS1 private keys to PKCS8", async (
7171

7272
test("worker GitHub App call sites preserve plain error messages", async () => {
7373
const originalFetch = globalThis.fetch;
74+
const originalSetTimeout = globalThis.setTimeout;
7475
const { privateKey } = generateKeyPairSync("rsa", {
7576
modulusLength: 2048,
7677
privateKeyEncoding: { type: "pkcs8", format: "pem" },
7778
publicKeyEncoding: { type: "spki", format: "pem" },
7879
});
80+
const fetchFailure = new TypeError("fetch failed");
81+
const abortFailure = new DOMException("request aborted", "AbortError");
7982
const failures = [
8083
{
81-
fetch: async () => {
82-
throw new DOMException("request aborted", "AbortError");
84+
fetch: async (_input: RequestInfo | URL, init?: RequestInit) => {
85+
assert.equal(init?.signal?.aborted, true);
86+
throw new DOMException("request timed out", "AbortError");
8387
},
88+
setTimeout: ((callback: TimerHandler) => {
89+
if (typeof callback === "function") callback();
90+
return 0;
91+
}) as typeof globalThis.setTimeout,
8492
message: "GitHub App installation timed out",
8593
constructor: Error,
8694
},
95+
{
96+
fetch: async () => {
97+
throw fetchFailure;
98+
},
99+
message: "fetch failed",
100+
constructor: TypeError,
101+
original: fetchFailure,
102+
},
103+
{
104+
fetch: async () => {
105+
throw abortFailure;
106+
},
107+
message: "request aborted",
108+
constructor: DOMException,
109+
original: abortFailure,
110+
},
87111
{
88112
fetch: async () =>
89113
new Response(JSON.stringify({ message: "upstream unavailable" }), { status: 503 }),
@@ -100,6 +124,7 @@ test("worker GitHub App call sites preserve plain error messages", async () => {
100124
try {
101125
for (const failure of failures) {
102126
globalThis.fetch = failure.fetch;
127+
globalThis.setTimeout = failure.setTimeout || originalSetTimeout;
103128
await assert.rejects(
104129
worker.fetch(workerGithubWebhookRequest(), {
105130
CLAWSWEEPER_APP_CLIENT_ID: "Iv23worker-errors",
@@ -109,12 +134,14 @@ test("worker GitHub App call sites preserve plain error messages", async () => {
109134
(error: Error) => {
110135
assert.equal(error.constructor, failure.constructor);
111136
assert.equal(error.message, failure.message);
137+
if (failure.original) assert.equal(error, failure.original);
112138
return true;
113139
},
114140
);
115141
}
116142
} finally {
117143
globalThis.fetch = originalFetch;
144+
globalThis.setTimeout = originalSetTimeout;
118145
}
119146
});
120147

0 commit comments

Comments
 (0)