Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 3 additions & 29 deletions src/app/api/webhooks/github/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { developerReceivesAISecurityExplanations } from '@/ai/flows/developer-re
import { App, Octokit } from 'octokit';
import { throttling } from '@octokit/plugin-throttling';
import prisma from '@/lib/prisma';
import { withErrorHandler } from '@/lib/middleware/error-handler';



Expand Down Expand Up @@ -50,13 +51,11 @@ async function verifyGitHubWebhook(req: NextRequest): Promise<any> {
return JSON.parse(payloadText);
}

export async function POST(req: NextRequest) {
export const POST = withErrorHandler(async function POST(req: NextRequest) {
let octokitInstance: any = null;
let checkRunId: number | null = null;
let repoOwner: string = '';
let repoName: string = '';

try {
const rawPayload = await verifyGitHubWebhook(req);

const deliveryId = req.headers.get('x-github-delivery');
Expand Down Expand Up @@ -543,29 +542,4 @@ export async function POST(req: NextRequest) {
}

return NextResponse.json({ message: 'Event successfully caught but ignored' }, { status: 200 });

} catch (error: any) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why have you removed the error handling tho?

console.error('Webhook Error:', error);

if (octokitInstance && checkRunId && repoOwner && repoName) {
try {
await octokitInstance.rest.checks.update({
owner: repoOwner,
repo: repoName,
check_run_id: checkRunId,
status: 'completed',
conclusion: 'failure',
completed_at: new Date().toISOString(),
output: {
title: 'Scan Failed: Webhook Exception',
summary: `SecureFlow encountered an unexpected error during execution. Error details: ${error.message || String(error)}`,
}
});
} catch (checkUpdateError) {
console.error('Failed to update GitHub check run on exception:', checkUpdateError);
}
}

return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
}
}
});
19 changes: 19 additions & 0 deletions src/lib/middleware/error-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { NextResponse } from 'next/server';

export function withErrorHandler(handler: Function) {
return async (...args: any[]) => {
try {
return await handler(...args);
} catch (error: any) {
// Strict sanitization: Never leak stack traces or env variables to the client
return NextResponse.json(
{
error: "Internal Server Error",
message: "An unexpected error occurred. Incident logged.",
success: false
},
{ status: 500 }
);
}
};
}
Loading