From 63ffe90e99963697da486210305743d8c3143c67 Mon Sep 17 00:00:00 2001 From: angelalvaigle Date: Sat, 14 Dec 2024 22:10:10 +0100 Subject: [PATCH] error handler middleware --- gatewayservice/gateway-service.js | 3 --- middleware/errorhandler-middleware.js | 11 +++++++++++ middleware/validation-middleware.js | 4 +++- statservice/stat-service.test.js | 2 +- users/userservice/user-service.test.js | 19 +++++++++++++++++++ webapp/src/pages/Profile.jsx | 3 +-- 6 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 middleware/errorhandler-middleware.js diff --git a/gatewayservice/gateway-service.js b/gatewayservice/gateway-service.js index 147759e..1cd6482 100644 --- a/gatewayservice/gateway-service.js +++ b/gatewayservice/gateway-service.js @@ -23,9 +23,6 @@ if (process.env.NODE_ENV === 'development') { originUrl = 'http://localhost:3000'; } -console.log('NODE_ENVL'); -console.log(process.env.NODE_ENV); - const authServiceUrl = process.env.AUTH_SERVICE_URL || 'http://localhost:8002'; const userServiceUrl = process.env.USER_SERVICE_URL || 'http://localhost:8001'; const questionServiceUrl = diff --git a/middleware/errorhandler-middleware.js b/middleware/errorhandler-middleware.js new file mode 100644 index 0000000..5f4dcff --- /dev/null +++ b/middleware/errorhandler-middleware.js @@ -0,0 +1,11 @@ +import { StatusCodes } from 'http-status-codes'; + +const errorHandlerMiddleware = (err, req, res, next) => { + console.log('error handler'); + console.log(err); + const statusCode = err.statusCode || StatusCodes.INTERNAL_SERVER_ERROR; + const msg = err.message || 'something went wrong'; + res.status(statusCode).json({ msg }); +}; + +export default errorHandlerMiddleware; diff --git a/middleware/validation-middleware.js b/middleware/validation-middleware.js index 31736f4..072a65e 100644 --- a/middleware/validation-middleware.js +++ b/middleware/validation-middleware.js @@ -1,4 +1,4 @@ -import { body, param, validationResult } from 'express-validator'; +import { body, validationResult } from 'express-validator'; import { BadRequestError } from '../errors/customErrors.js'; import User from '../user-model.js'; @@ -9,6 +9,8 @@ const withValidationErrors = (validateValues) => { const errors = validationResult(req); if (!errors.isEmpty()) { const errorMessages = errors.array().map((error) => error.msg); + console.log('validation mw'); + console.log(errorMessages); throw new BadRequestError(errorMessages); } next(); diff --git a/statservice/stat-service.test.js b/statservice/stat-service.test.js index 28725ad..a0ec2c4 100644 --- a/statservice/stat-service.test.js +++ b/statservice/stat-service.test.js @@ -1,6 +1,6 @@ import request from 'supertest'; import { MongoMemoryServer } from 'mongodb-memory-server'; -import { jest } from '@jest/globals'; // Importa jest desde @jest/globals +import { jest } from '@jest/globals'; // Sobrescribe `authenticateUser` antes de importar el servicio jest.unstable_mockModule('./middleware/auth-middleware', () => ({ diff --git a/users/userservice/user-service.test.js b/users/userservice/user-service.test.js index 0cac208..5217b33 100644 --- a/users/userservice/user-service.test.js +++ b/users/userservice/user-service.test.js @@ -1,5 +1,24 @@ import request from 'supertest'; import { MongoMemoryServer } from 'mongodb-memory-server'; +import { jest } from '@jest/globals'; + +// Sobrescribe `authenticateUser` antes de importar el servicio +jest.unstable_mockModule('./middleware/auth-middleware', () => ({ + authenticateUser: jest.fn((req, res, next) => { + req.user = { userId: '507f1f77bcf86cd799439010', role: 'user' }; + next(); + }), +})); + +// Sobrescribe `validateRegisterInput` antes de importar el servicio +jest.unstable_mockModule('./middleware/validation-middleware', () => ({ + validateRegisterInput: jest.fn((req, res, next) => { + next(); + }), + validateUpdateUserInput: jest.fn((req, res, next) => { + next(); + }), +})); let mongoServer; let app; diff --git a/webapp/src/pages/Profile.jsx b/webapp/src/pages/Profile.jsx index 31ba009..72e49bf 100644 --- a/webapp/src/pages/Profile.jsx +++ b/webapp/src/pages/Profile.jsx @@ -42,9 +42,8 @@ const Profile = () => { ); setOpenSnackbar(true); } catch (error) { - setError(error || 'An error occurred'); + setError(error?.response?.data?.msg || 'An error occurred'); } - return null; }; const handleCloseSnackbar = () => {