Skip to content

Commit

Permalink
error handler middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
angelalvaigle committed Dec 14, 2024
1 parent 6ae9154 commit 63ffe90
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 7 deletions.
3 changes: 0 additions & 3 deletions gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
11 changes: 11 additions & 0 deletions middleware/errorhandler-middleware.js
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 3 additions & 1 deletion middleware/validation-middleware.js
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion statservice/stat-service.test.js
Original file line number Diff line number Diff line change
@@ -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', () => ({
Expand Down
19 changes: 19 additions & 0 deletions users/userservice/user-service.test.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
3 changes: 1 addition & 2 deletions webapp/src/pages/Profile.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down

0 comments on commit 63ffe90

Please sign in to comment.