From 8822c5840dafa75b93519991cef5f27fbd8ffdd3 Mon Sep 17 00:00:00 2001 From: Tim Paine <3105306+timkpaine@users.noreply.github.com> Date: Tue, 11 Aug 2026 18:18:17 -0400 Subject: [PATCH] Log webhook error messages Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com> --- src/index.ts | 4 ++-- tests/index.test.ts | 23 ++++++++++++++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 3c188af..3982835 100644 --- a/src/index.ts +++ b/src/index.ts @@ -23,7 +23,7 @@ export async function handleRequest(request: Request, env: Env, context?: Webhoo if (context) { context.waitUntil( processing.catch((error) => { - console.error(error); + console.error(error instanceof Error ? error.message : error); }), ); return new Response(null, { status: 202 }); @@ -32,7 +32,7 @@ export async function handleRequest(request: Request, env: Env, context?: Webhoo await processing; return new Response(null, { status: 202 }); } catch (error) { - console.error(error); + console.error(error instanceof Error ? error.message : error); return new Response(error instanceof Error ? error.message : "Webhook processing failed", { status: 500 }); } } diff --git a/tests/index.test.ts b/tests/index.test.ts index d9b2585..e9d8e88 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -1,6 +1,6 @@ import { createHmac } from "node:crypto"; -import { describe, expect, test, vi } from "vitest"; +import { afterEach, describe, expect, test, vi } from "vitest"; import { handleRequest } from "../src/index"; @@ -22,6 +22,10 @@ function webhook(body: string, event: string, signature?: string): Request { }); } +afterEach(() => { + vi.restoreAllMocks(); +}); + describe("worker", () => { test.each([ ["/", "Merge when GitHub says it is ready."], @@ -69,6 +73,23 @@ describe("worker", () => { await context.waitUntil.mock.calls[0][0]; }); + test("logs asynchronous webhook error messages", async () => { + const errorMock = vi.spyOn(console, "error").mockImplementation(() => undefined); + const body = JSON.stringify({ + action: "synchronize", + installation: { id: 1 }, + repository: { id: 1, full_name: "owner/repository" }, + pull_request: { number: 1, labels: [{ name: "automerge" }] }, + }); + const context = { waitUntil: vi.fn() }; + + const response = await handleRequest(webhook(body, "pull_request"), env, context); + await context.waitUntil.mock.calls[0][0]; + + expect(response.status).toBe(202); + expect(errorMock).toHaveBeenCalledWith("GitHub App private key must be an RSA PEM private key"); + }); + test("reports webhook processing errors", async () => { const body = JSON.stringify({ action: "synchronize",