From 1728a75b194b2f027607e444459e8b3391a180cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl-Gerhard=20Lindesva=CC=88rd?= Date: Wed, 17 Jul 2024 22:52:14 +0200 Subject: [PATCH] improve sdk error logging --- apps/api/src/routes/event.router.ts | 6 ++++-- apps/api/src/routes/profile.router.ts | 6 ++++-- apps/api/src/utils/auth.ts | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/apps/api/src/routes/event.router.ts b/apps/api/src/routes/event.router.ts index c48e1e50..ed23ecf9 100644 --- a/apps/api/src/routes/event.router.ts +++ b/apps/api/src/routes/event.router.ts @@ -1,6 +1,6 @@ import { isBot } from '@/bots'; import * as controller from '@/controllers/event.controller'; -import { validateSdkRequest } from '@/utils/auth'; +import { SdkAuthError, validateSdkRequest } from '@/utils/auth'; import { logger } from '@/utils/logger'; import type { FastifyPluginCallback, FastifyRequest } from 'fastify'; @@ -18,7 +18,9 @@ const eventRouter: FastifyPluginCallback = (fastify, opts, done) => { ) => { try { const client = await validateSdkRequest(req.headers).catch((error) => { - logger.error(error, 'Failed to validate sdk request'); + if (!(error instanceof SdkAuthError)) { + logger.error(error, 'Failed to validate sdk request'); + } return null; }); if (!client?.projectId) { diff --git a/apps/api/src/routes/profile.router.ts b/apps/api/src/routes/profile.router.ts index 2c9f1111..891ddeaf 100644 --- a/apps/api/src/routes/profile.router.ts +++ b/apps/api/src/routes/profile.router.ts @@ -1,6 +1,6 @@ import { isBot } from '@/bots'; import * as controller from '@/controllers/profile.controller'; -import { validateSdkRequest } from '@/utils/auth'; +import { SdkAuthError, validateSdkRequest } from '@/utils/auth'; import { logger } from '@/utils/logger'; import type { FastifyPluginCallback } from 'fastify'; @@ -8,7 +8,9 @@ const eventRouter: FastifyPluginCallback = (fastify, opts, done) => { fastify.addHook('preHandler', async (req, reply) => { try { const client = await validateSdkRequest(req.headers).catch((error) => { - logger.error(error, 'Failed to validate sdk request'); + if (!(error instanceof SdkAuthError)) { + logger.error(error, 'Failed to validate sdk request'); + } return null; }); if (!client?.projectId) { diff --git a/apps/api/src/utils/auth.ts b/apps/api/src/utils/auth.ts index 83c2574f..43fd9b38 100644 --- a/apps/api/src/utils/auth.ts +++ b/apps/api/src/utils/auth.ts @@ -11,7 +11,7 @@ const cleanDomain = (domain: string) => .replace(/https?:\/\//, '') .replace(/\/$/, ''); -class SdkAuthError extends Error { +export class SdkAuthError extends Error { payload: { clientId?: string; clientSecret?: string;