diff --git a/apps/api/src/lib/common.ts b/apps/api/src/lib/common.ts index 40fd6163f..a55aa1c7b 100644 --- a/apps/api/src/lib/common.ts +++ b/apps/api/src/lib/common.ts @@ -1486,13 +1486,17 @@ export function makeLabelForServices(type) { } export function errorHandler({ status = 500, - message = 'Unknown error.' + message = 'Unknown error.', + type = 'normal' }: { status: number; message: string | any; + type?: string | null; }) { if (message.message) message = message.message; - Sentry.captureException(message); + if (type === 'normal') { + Sentry.captureException(message); + } throw { status, message }; } export async function generateSshKeyPair(): Promise<{ publicKey: string; privateKey: string }> { diff --git a/apps/api/src/routes/api/v1/applications/handlers.ts b/apps/api/src/routes/api/v1/applications/handlers.ts index 3ee5858ec..a0e383398 100644 --- a/apps/api/src/routes/api/v1/applications/handlers.ts +++ b/apps/api/src/routes/api/v1/applications/handlers.ts @@ -281,7 +281,7 @@ export async function getApplicationFromDBWebhook(projectId: number, branch: str } }); if (applications.length === 0) { - throw { status: 500, message: 'Application not configured.' } + throw { status: 500, message: 'Application not configured.', type: 'webhook' } } applications = applications.map((application: any) => { application = decryptApplication(application); @@ -303,8 +303,8 @@ export async function getApplicationFromDBWebhook(projectId: number, branch: str return applications; - } catch ({ status, message }) { - return errorHandler({ status, message }) + } catch ({ status, message, type }) { + return errorHandler({ status, message, type }) } } export async function saveApplication(request: FastifyRequest, reply: FastifyReply) { diff --git a/apps/api/src/routes/webhooks/github/handlers.ts b/apps/api/src/routes/webhooks/github/handlers.ts index bfb3f3db8..5e0f2bbba 100644 --- a/apps/api/src/routes/webhooks/github/handlers.ts +++ b/apps/api/src/routes/webhooks/github/handlers.ts @@ -71,7 +71,7 @@ export async function gitHubEvents(request: FastifyRequest): Promi const githubEvent = request.headers['x-github-event']?.toString().toLowerCase(); const githubSignature = request.headers['x-hub-signature-256']?.toString().toLowerCase(); if (!allowedGithubEvents.includes(githubEvent)) { - throw { status: 500, message: 'Event not allowed.' } + throw { status: 500, message: 'Event not allowed.', type: 'webhook' } } if (githubEvent === 'ping') { return { pong: 'cool' } @@ -89,7 +89,7 @@ export async function gitHubEvents(request: FastifyRequest): Promi branch = body.pull_request.base.ref } if (!projectId || !branch) { - throw { status: 500, message: 'Cannot parse projectId or branch from the webhook?!' } + throw { status: 500, message: 'Cannot parse projectId or branch from the webhook?!', type: 'webhook' } } const applicationsFound = await getApplicationFromDBWebhook(projectId, branch); const settings = await prisma.setting.findUnique({ where: { id: '0' } }); @@ -107,7 +107,7 @@ export async function gitHubEvents(request: FastifyRequest): Promi const checksum = Buffer.from(githubSignature, 'utf8'); //@ts-ignore if (checksum.length !== digest.length || !crypto.timingSafeEqual(digest, checksum)) { - throw { status: 500, message: 'SHA256 checksum failed. Are you doing something fishy?' } + throw { status: 500, message: 'SHA256 checksum failed. Are you doing something fishy?', type: 'webhook' } }; } @@ -157,7 +157,7 @@ export async function gitHubEvents(request: FastifyRequest): Promi const sourceBranch = body.pull_request.head.ref const sourceRepository = body.pull_request.head.repo.full_name if (!allowedActions.includes(pullmergeRequestAction)) { - throw { status: 500, message: 'Action not allowed.' } + throw { status: 500, message: 'Action not allowed.', type: 'webhook' } } if (application.settings.previews) { @@ -169,7 +169,7 @@ export async function gitHubEvents(request: FastifyRequest): Promi } ); if (!isRunning) { - throw { status: 500, message: 'Application not running.' } + throw { status: 500, message: 'Application not running.', type: 'webhook' } } } if ( @@ -258,8 +258,8 @@ export async function gitHubEvents(request: FastifyRequest): Promi } } } - } catch ({ status, message }) { - return errorHandler({ status, message }) + } catch ({ status, message, type }) { + return errorHandler({ status, message, type }) } } \ No newline at end of file diff --git a/apps/api/src/routes/webhooks/gitlab/handlers.ts b/apps/api/src/routes/webhooks/gitlab/handlers.ts index 52bc2fda2..a3ac97705 100644 --- a/apps/api/src/routes/webhooks/gitlab/handlers.ts +++ b/apps/api/src/routes/webhooks/gitlab/handlers.ts @@ -44,7 +44,7 @@ export async function gitLabEvents(request: FastifyRequest) { const allowedActions = ['opened', 'reopen', 'close', 'open', 'update']; const webhookToken = request.headers['x-gitlab-token']; if (!webhookToken && !isDev) { - throw { status: 500, message: 'Invalid webhookToken.' } + throw { status: 500, message: 'Invalid webhookToken.', type: 'webhook' } } const settings = await prisma.setting.findUnique({ where: { id: '0' } }); if (objectKind === 'push') { @@ -96,10 +96,10 @@ export async function gitLabEvents(request: FastifyRequest) { const pullmergeRequestId = request.body.object_attributes.iid.toString(); const projectId = Number(id); if (!allowedActions.includes(action)) { - throw { status: 500, message: 'Action not allowed.' } + throw { status: 500, message: 'Action not allowed.', type: 'webhook' } } if (isDraft) { - throw { status: 500, message: 'Draft MR, do nothing.' } + throw { status: 500, message: 'Draft MR, do nothing.', type: 'webhook' } } const applicationsFound = await getApplicationFromDBWebhook(projectId, targetBranch); if (applicationsFound && applicationsFound.length > 0) { @@ -114,11 +114,11 @@ export async function gitLabEvents(request: FastifyRequest) { } ); if (!isRunning) { - throw { status: 500, message: 'Application not running.' } + throw { status: 500, message: 'Application not running.', type: 'webhook' } } } if (!isDev && application.gitSource.gitlabApp.webhookToken !== webhookToken) { - throw { status: 500, message: 'Invalid webhookToken. Are you doing something nasty?!' } + throw { status: 500, message: 'Invalid webhookToken. Are you doing something nasty?!', type: 'webhook' } } if ( action === 'opened' || @@ -189,7 +189,7 @@ export async function gitLabEvents(request: FastifyRequest) { } } } - } catch ({ status, message }) { - return errorHandler({ status, message }) + } catch ({ status, message, type }) { + return errorHandler({ status, message, type }) } } \ No newline at end of file