diff --git a/api/src/controller/http/middleware.ts b/api/src/controller/http/middleware.ts index 5394e48e1..5a2afe5bc 100644 --- a/api/src/controller/http/middleware.ts +++ b/api/src/controller/http/middleware.ts @@ -3,6 +3,8 @@ import 'express-async-errors'; import { HttpError as ValidationError } from 'express-openapi-validator/dist/framework/types'; import { Logger } from 'winston'; +import { getErrorMessage } from '@src/util/error'; + import { BadRequestErrorType, CustomError, @@ -56,7 +58,7 @@ export class Middleware { } else { customError = new CustomError( InternalErrorType.UNEXPECTED, - 'Unexpected error happened' + `Unexpected error occured, error: ${getErrorMessage(err)}` ); } @@ -129,6 +131,21 @@ export class Middleware { ) => { const statusCode = this.getStatusCode(err); + switch (true) { + case statusCode >= 500: + this.logger.error('5xx error occured', { + error: err, + statusCode: statusCode + }); + break; + case statusCode >= 400: + this.logger.warn('4xx error occured', { + error: err, + statusCode: statusCode + }); + break; + } + res.locals.error = err; res.status(statusCode); res.send({ diff --git a/api/test/controller/http/middleware.test.ts b/api/test/controller/http/middleware.test.ts index a2791cf8d..71cb1a91d 100644 --- a/api/test/controller/http/middleware.test.ts +++ b/api/test/controller/http/middleware.test.ts @@ -176,7 +176,7 @@ describe('Test middleware', () => { let baseUrl: string; beforeAll(async () => { - mockLogger = {}; + mockLogger = { error: jest.fn(), warn: jest.fn() }; testHttpServer = new TestHttpServer(mockLogger as Logger); baseUrl = '/api/v1/dev'; await testHttpServer.start(); @@ -215,7 +215,7 @@ describe('Test middleware', () => { expect(response.status).toEqual(500); expect(response.body).toStrictEqual({ type: InternalErrorType.UNEXPECTED, - messages: ['Unexpected error happened'] + messages: ['Unexpected error occured, error: Error from throwSyncError'] }); }); @@ -226,7 +226,7 @@ describe('Test middleware', () => { expect(response.status).toEqual(500); expect(response.body).toStrictEqual({ type: InternalErrorType.UNEXPECTED, - messages: ['Unexpected error happened'] + messages: ['Unexpected error occured, error: Error from throwAsyncError'] }); });