Skip to content

Commit 62c8a2a

Browse files
committed
Ajout de la gestion de l'ID de requête et de la pile d'erreurs dans les réponses des fonctions
1 parent b222c12 commit 62c8a2a

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

src/main.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type WorkerResponse = {
2929
body: string;
3030
};
3131
error?: string;
32+
errorStack?: string;
3233
};
3334

3435
const timeoutMs = Number(Deno.env.get("RUNHOOK_TIMEOUT_MS") ?? "1500");
@@ -166,15 +167,28 @@ async function handleWebhook(req: Request, url: URL, path: string): Promise<Resp
166167
now: new Date().toISOString(),
167168
};
168169

170+
const requestId = crypto.randomUUID();
171+
169172
try {
170-
const response = await runFunction(func.code, ctx);
173+
const response = await runFunction(func.code, ctx, requestId);
174+
const headers = new Headers(response.headers);
175+
headers.set("x-runhook-request-id", requestId);
171176
return new Response(response.body, {
172177
status: response.status,
173-
headers: response.headers,
178+
headers,
174179
});
175180
} catch (err) {
176181
const message = err instanceof Error ? err.message : String(err);
177-
return jsonResponse({ error: message }, 500);
182+
const stack = err instanceof Error ? err.stack : undefined;
183+
console.error("function execution failed", {
184+
requestId,
185+
name,
186+
error: message,
187+
stack,
188+
});
189+
const resp = jsonResponse({ error: message, requestId }, 500);
190+
resp.headers.set("x-runhook-request-id", requestId);
191+
return resp;
178192
}
179193
}
180194

@@ -193,7 +207,7 @@ function handleMcp(req: Request, path: string): Response | Promise<Response> {
193207
return jsonResponse({ error: "not found" }, 404);
194208
}
195209

196-
async function runFunction(code: string, ctx: ExecContext): Promise<{
210+
async function runFunction(code: string, ctx: ExecContext, requestId: string): Promise<{
197211
status: number;
198212
headers: Record<string, string>;
199213
body: string;
@@ -212,15 +226,14 @@ async function runFunction(code: string, ctx: ExecContext): Promise<{
212226
},
213227
});
214228

215-
const id = crypto.randomUUID();
216229
const result = await new Promise<WorkerResponse>((resolve, reject) => {
217230
const timer = setTimeout(() => {
218231
worker.terminate();
219232
reject(new Error("execution timeout"));
220233
}, timeoutMs);
221234

222235
worker.onmessage = (event: MessageEvent<WorkerResponse>) => {
223-
if (event.data?.id !== id) return;
236+
if (event.data?.id !== requestId) return;
224237
clearTimeout(timer);
225238
resolve(event.data);
226239
};
@@ -230,12 +243,18 @@ async function runFunction(code: string, ctx: ExecContext): Promise<{
230243
reject(new Error(event.message));
231244
};
232245

233-
worker.postMessage({ id, code, ctx });
246+
worker.postMessage({ id: requestId, code, ctx });
234247
});
235248

236249
worker.terminate();
237250

238-
if (!result.ok) throw new Error(result.error || "execution error");
251+
if (!result.ok) {
252+
const err = new Error(result.error || "execution error");
253+
if (result.errorStack) {
254+
err.stack = result.errorStack;
255+
}
256+
throw err;
257+
}
239258
if (!result.response) throw new Error("empty response");
240259
return result.response;
241260
}

src/worker.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type WorkerResponse = {
1313
body: string;
1414
};
1515
error?: string;
16+
errorStack?: string;
1617
};
1718

1819
self.onmessage = async (event: MessageEvent<WorkerRequest>) => {
@@ -35,7 +36,8 @@ self.onmessage = async (event: MessageEvent<WorkerRequest>) => {
3536
self.postMessage(message);
3637
} catch (err) {
3738
const error = err instanceof Error ? err.message : String(err);
38-
const message: WorkerResponse = { id, ok: false, error };
39+
const errorStack = err instanceof Error ? err.stack : undefined;
40+
const message: WorkerResponse = { id, ok: false, error, errorStack };
3941
self.postMessage(message);
4042
}
4143
};

0 commit comments

Comments
 (0)