Correction

This commit is contained in:
NouhailaAater 2025-03-02 01:56:37 -05:00
parent 9ece28a86b
commit 545d6551f6
4 changed files with 27 additions and 39 deletions

18
.gitignore vendored
View file

@ -122,6 +122,9 @@ dist
# Stores VSCode versions used for testing VSCode extensions # Stores VSCode versions used for testing VSCode extensions
.vscode-test .vscode-test
.env
launch.json
# yarn v2 # yarn v2
.yarn/cache .yarn/cache
.yarn/unplugged .yarn/unplugged
@ -129,18 +132,3 @@ dist
.yarn/install-state.gz .yarn/install-state.gz
.pnp.* .pnp.*
db-backup/ db-backup/
.V5/
.EvaluateTonSavoir/
config/
applicationhost.config
FileContentIndex/
*.vsidx
v17/
.WSuO/
DocumentLayout.backup.json
DocumentLayout.json
ProjectSettings.json
slnx.sqlite
slnx.sqlite-journal
VSWorkspaceState.json

View file

@ -17,7 +17,7 @@ class RoomsController {
create = async (req, res, next) => { create = async (req, res, next) => {
try { try {
if (!req.user || !req.user.userId) { if (!req.user || !req.user.userId) {
throw new AppError("Utilisateur non authentifié", 401); throw new AppError(MISSING_REQUIRED_PARAMETER);
} }
const { title } = req.body; const { title } = req.body;
@ -30,9 +30,7 @@ class RoomsController {
const roomExists = await this.rooms.roomExists(normalizedTitle); const roomExists = await this.rooms.roomExists(normalizedTitle);
if (roomExists) { if (roomExists) {
const error = new Error("Room already exists"); throw new AppError(ROOM_ALREADY_EXISTS);
error.statusCode = 409;
throw error;
} }
const result = await this.rooms.create(normalizedTitle, req.user.userId); const result = await this.rooms.create(normalizedTitle, req.user.userId);
@ -42,17 +40,14 @@ class RoomsController {
roomId: result.insertedId, roomId: result.insertedId,
}); });
} catch (error) { } catch (error) {
if (error instanceof Error) { next(
if (error.message === "Room already exists") { error instanceof AppError
return next(new AppError(ROOM_ALREADY_EXISTS)); ? error
} : new AppError({ message: error.message, code: 500 })
} );
next(error);
} }
}; };
getUserRooms = async (req, res, next) => { getUserRooms = async (req, res, next) => {
try { try {
const rooms = await this.rooms.getUserRooms(req.user.userId); const rooms = await this.rooms.getUserRooms(req.user.userId);

View file

@ -1,9 +1,10 @@
class AppError extends Error { class AppError extends Error {
constructor (errorCode) { constructor (errorCode) {
super(errorCode.message) super(errorCode.message);
this.statusCode = errorCode.code; this.statusCode = errorCode.code;
this.isOperational = true; // Optional: to distinguish operational errors from programming errors this.isOperational = true;
} }
} }
module.exports = AppError; module.exports = AppError;

View file

@ -10,10 +10,14 @@ const errorHandler = (error, req, res, _next) => {
}); });
} }
return res.status(500).json({ logError(error.stack);
message: "Erreur technique", return res.status(505).send("Oups! We screwed up big time. ┻━┻ ︵ヽ(`Д´)ノ︵ ┻━┻");
code: 500
});
}; };
const logError = (error) => {
const time = new Date();
var log_file = fs.createWriteStream(__dirname + '/../debug.log', {flags : 'a'});
log_file.write(time + '\n' + error + '\n\n');
}
module.exports = errorHandler; module.exports = errorHandler;