From a905083bf88887b14de64e0843e3d3e03912b7a8 Mon Sep 17 00:00:00 2001 From: Mrugesh Mohapatra <1884376+raisedadead@users.noreply.github.com> Date: Fri, 25 Apr 2025 16:04:03 +0530 Subject: [PATCH] chore(api): improve log statements (#59992) --- api/src/plugins/error-handling.test.ts | 10 ++++++++-- api/src/plugins/error-handling.ts | 4 ++-- api/src/routes/protected/challenge.ts | 6 +++--- api/src/routes/protected/donate.ts | 6 ++---- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/api/src/plugins/error-handling.test.ts b/api/src/plugins/error-handling.test.ts index 48a2f20a89f..d437b94dbef 100644 --- a/api/src/plugins/error-handling.test.ts +++ b/api/src/plugins/error-handling.test.ts @@ -170,7 +170,10 @@ describe('errorHandling', () => { url: '/test' }); - expect(logSpy).toHaveBeenCalledWith(Error('a very bad thing happened')); + expect(logSpy).toHaveBeenCalledWith( + Error('a very bad thing happened'), + 'Error in request' + ); }); it('should call fastify.log.warn when a bad request error occurs', async () => { @@ -181,7 +184,10 @@ describe('errorHandling', () => { url: '/test-bad-request' }); - expect(logSpy).toHaveBeenCalledWith(Error('a very bad thing happened')); + expect(logSpy).toHaveBeenCalledWith( + Error('a very bad thing happened'), + 'CSRF error in request' + ); }); it('should NOT log when a CSRF error is thrown', async () => { diff --git a/api/src/plugins/error-handling.ts b/api/src/plugins/error-handling.ts index f40cd12a818..eaf812d8bfb 100644 --- a/api/src/plugins/error-handling.ts +++ b/api/src/plugins/error-handling.ts @@ -39,9 +39,9 @@ const errorHandling: FastifyPluginCallback = (fastify, _options, done) => { if (!isCSRFError) { if (reply.statusCode >= 500) { - logger.error(error); + logger.error(error, 'Error in request'); } else { - logger.warn(error); + logger.warn(error, 'CSRF error in request'); } } diff --git a/api/src/routes/protected/challenge.ts b/api/src/routes/protected/challenge.ts index 8cd440ca465..2bec33749a8 100644 --- a/api/src/routes/protected/challenge.ts +++ b/api/src/routes/protected/challenge.ts @@ -187,7 +187,7 @@ export const challengeRoutes: FastifyPluginCallbackTypebox = ( } } catch (error) { // TODO(Post-MVP): don't catch, just let Sentry handle this. - logger.error(error); + logger.error(error, 'Error submitting coderoad challenge'); fastify.Sentry.captureException(error); void reply.code(400); return { @@ -669,7 +669,7 @@ export const challengeRoutes: FastifyPluginCallbackTypebox = ( completedDate }; } catch (error) { - logger.error(error); + logger.error(error, 'Error submitting Microsoft trophy challenge'); fastify.Sentry.captureException(error); void reply.code(500); return { @@ -884,7 +884,7 @@ export const challengeRoutes: FastifyPluginCallbackTypebox = ( examResults }; } catch (error) { - logger.error(error); + logger.error(error, 'Error submitting exam challenge'); fastify.Sentry.captureException(error); void reply.code(500); return { diff --git a/api/src/routes/protected/donate.ts b/api/src/routes/protected/donate.ts index b1ca1c223d7..887b454784d 100644 --- a/api/src/routes/protected/donate.ts +++ b/api/src/routes/protected/donate.ts @@ -90,8 +90,7 @@ export const donateRoutes: FastifyPluginCallbackTypebox = ( isDonating: true } as const; } catch (error) { - logger.error(`User ${req.user?.id} failed to donate`); - logger.error(error); + logger.error(error, `User ${req.user?.id} failed to donate`); fastify.Sentry.captureException(error); void reply.code(500); return { @@ -227,8 +226,7 @@ export const donateRoutes: FastifyPluginCallbackTypebox = ( isDonating: true }); } catch (error) { - logger.error(`User ${req.user?.id} failed to donate`); - logger.error(error); + logger.error(error, `User ${req.user?.id} failed to donate`); fastify.Sentry.captureException(error); void reply.code(500); return reply.send({