From 5d7bb48b4e941a97e1f8da86f5e1be11b7b8201a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl-Gerhard=20Lindesva=CC=88rd?= Date: Sat, 7 Dec 2024 21:05:06 +0100 Subject: [PATCH] fix(api): logs --- apps/api/src/hooks/client.hook.ts | 12 +++++++++--- apps/api/src/utils/auth.ts | 1 + 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/apps/api/src/hooks/client.hook.ts b/apps/api/src/hooks/client.hook.ts index 4ec5ced2..42aaac98 100644 --- a/apps/api/src/hooks/client.hook.ts +++ b/apps/api/src/hooks/client.hook.ts @@ -1,17 +1,23 @@ import { SdkAuthError, validateSdkRequest } from '@/utils/auth'; +import type { PostEventPayload, TrackHandlerPayload } from '@openpanel/sdk'; import type { FastifyReply, FastifyRequest } from 'fastify'; -export async function clientHook(req: FastifyRequest, reply: FastifyReply) { +export async function clientHook( + req: FastifyRequest<{ + Body: PostEventPayload | TrackHandlerPayload; + }>, + reply: FastifyReply, +) { try { const client = await validateSdkRequest(req); req.client = client; } catch (error) { if (error instanceof SdkAuthError) { - req.log.warn(error, 'Invalid SDK request'); + req.log.warn('Invalid SDK request', error); return reply.status(401).send(error.message); } - req.log.error(error, 'Invalid SDK request'); + req.log.error('Invalid SDK request', error); return reply.status(500).send('Internal server error'); } } diff --git a/apps/api/src/utils/auth.ts b/apps/api/src/utils/auth.ts index 606abb1b..26f0b608 100644 --- a/apps/api/src/utils/auth.ts +++ b/apps/api/src/utils/auth.ts @@ -38,6 +38,7 @@ export class SdkAuthError extends Error { ) { super(message); this.name = 'SdkAuthError'; + this.message = message; this.payload = payload; } }