diff --git a/srcs/requirements/nestjs/api_back/src/chat/chat.controller.ts b/srcs/requirements/nestjs/api_back/src/chat/chat.controller.ts index 81037ddc..5911f152 100644 --- a/srcs/requirements/nestjs/api_back/src/chat/chat.controller.ts +++ b/srcs/requirements/nestjs/api_back/src/chat/chat.controller.ts @@ -120,7 +120,7 @@ export class ChatController { { let forbidden_chars = room.name.replace(new RegExp(regex_base, "g"), ""); printCaller(`throw error: error: true, code: 'FORBIDDEN_CHARACTERS', message: 'Your room name can not contains these characters : ${forbidden_chars}'`); - throw new HttpException({ error: true, code: 'FORBIDDEN_CHARACTERS', message: `Your room name can not contains these characters : ${forbidden_chars}` }, HttpStatus.OK); + throw new HttpException({ error: true, code: 'FORBIDDEN_CHARACTERS', message: `Your room name can not contains these characters : ${forbidden_chars}` }, HttpStatus.UNPROCESSABLE_ENTITY); } if (typeof room.protection === 'undefined') @@ -130,7 +130,7 @@ export class ChatController { if (!room.password || room.password.length === 0) { printCaller(`throw error: error: true, code: 'PASSWORD_TOO_SHORT', message: 'your password is too short'`); - throw new HttpException({ error: true, code: 'PASSWORD_TOO_SHORT', message: `your password is too short` }, HttpStatus.OK); + throw new HttpException({ error: true, code: 'PASSWORD_TOO_SHORT', message: `your password is too short` }, HttpStatus.UNPROCESSABLE_ENTITY); } } room.users = [req.user.username]; @@ -164,17 +164,17 @@ export class ChatController { if (room_db.type === 'direct') { console.log("throw error: error: true, code: 'JOIN_DIRECT_FORBIDDEN', message: 'cannot join a direct messages room'"); - throw new HttpException({ error: true, code: 'JOIN_DIRECT_FORBIDDEN', message: `cannot join a direct messages room` }, HttpStatus.OK); + throw new HttpException({ error: true, code: 'JOIN_DIRECT_FORBIDDEN', message: `cannot join a direct messages room` }, HttpStatus.CONFLICT); } if (room_db.type === 'private') { console.log("throw error: error: true, code: 'JOIN_PRIVATE_FORBIDDEN', message: 'cannot join a private room'"); - throw new HttpException({ error: true, code: 'JOIN_PRIVATE_FORBIDDEN', message: `cannot join a private room` }, HttpStatus.OK); + throw new HttpException({ error: true, code: 'JOIN_PRIVATE_FORBIDDEN', message: `cannot join a private room` }, HttpStatus.CONFLICT); } if (room_db.users.includes(req.user.username)) { console.log("throw error: error: true, code: 'ALREADY_JOIN', message: 'your have already joined this room'"); - throw new HttpException({ error: true, code: 'ALREADY_JOIN', message: `your have already joined this room` }, HttpStatus.OK); + throw new HttpException({ error: true, code: 'ALREADY_JOIN', message: `your have already joined this room` }, HttpStatus.CONFLICT); } room = await this.chatService.addUserToRoom(req.user.username, room.name); } @@ -201,7 +201,7 @@ export class ChatController { if (!room_db.allowed_users.includes(req.user.username)) { console.log("throw error: error: true, code: 'NEED_AUTHENTICATE', message: 'You didn't provide the password for this room'"); - throw new HttpException({ error: true, code: 'NEED_AUTHENTICATE', message: `You didn't provide the password for this room` }, HttpStatus.OK); + throw new HttpException({ error: true, code: 'NEED_AUTHENTICATE', message: `You didn't provide the password for this room` }, HttpStatus.UNAUTHORIZED); } } @@ -228,7 +228,7 @@ export class ChatController { if (!room.password) { console.log("throw error: error: true, code: 'PASSWORD_MISSING', message: 'this room is protected, you need to provide a password'"); - throw new HttpException({ error: true, code: 'PASSWORD_MISSING', message: `this room is protected, you need to provide a password` }, HttpStatus.OK); + throw new HttpException({ error: true, code: 'PASSWORD_MISSING', message: `this room is protected, you need to provide a password` }, HttpStatus.UNAUTHORIZED); } if (!room_db.allowed_users.includes(req.user.username)) await this.chatService.setPasswordValidation(req.user.username, room); diff --git a/srcs/requirements/nestjs/api_back/src/chat/chat.service.ts b/srcs/requirements/nestjs/api_back/src/chat/chat.service.ts index 066a7361..e0dde777 100644 --- a/srcs/requirements/nestjs/api_back/src/chat/chat.service.ts +++ b/srcs/requirements/nestjs/api_back/src/chat/chat.service.ts @@ -218,7 +218,7 @@ export class ChatService { if (find_room) { console.log("throw error: error: true, code: 'ROOM_CONFLICT', message: 'This room name already exist'"); - throw new HttpException({ error: true, code: 'ROOM_CONFLICT', message: `This room name already exist` }, HttpStatus.OK); + throw new HttpException({ error: true, code: 'ROOM_CONFLICT', message: `This room name already exist` }, HttpStatus.CONFLICT); } let hash; @@ -228,7 +228,7 @@ export class ChatService { if (room.type === 'direct') { console.log("throw error: error: true, code: 'DIRECT_PASSWORD_FORBIDDEN', message: 'you cannot set a password in a direct message room'"); - throw new HttpException({ error: true, code: 'DIRECT_PASSWORD_FORBIDDEN', message: `you cannot set a password in a direct message room`}, HttpStatus.OK); + throw new HttpException({ error: true, code: 'DIRECT_PASSWORD_FORBIDDEN', message: `you cannot set a password in a direct message room`}, HttpStatus.FORBIDDEN); } const saltOrRounds = 10; const password = room.password; @@ -298,12 +298,12 @@ export class ChatService { if (!room.users.includes(username)) { console.log("throw error: error: true, code: 'USER_NOT_FOUND', message: 'your are not in this room'"); - throw new HttpException({ error: true, code: 'USER_NOT_FOUND', message: `your are not in this room` }, HttpStatus.OK); + throw new HttpException({ error: true, code: 'USER_NOT_FOUND', message: `your are not in this room` }, HttpStatus.NOT_FOUND); } if (room.type === "direct") { console.log("throw error: error: true, code: 'LEAVE_DIRECY_FORBIDDEN', message: 'you cannot leave a direct messages conversation'"); - throw new HttpException({ error: true, code: 'LEAVE_DIRECY_FORBIDDEN', message: `you cannot leave a direct messages conversation` }, HttpStatus.OK); + throw new HttpException({ error: true, code: 'LEAVE_DIRECY_FORBIDDEN', message: `you cannot leave a direct messages conversation` }, HttpStatus.FORBIDDEN); } // delete user from room diff --git a/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_create.svelte b/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_create.svelte index b23f1179..99030f01 100644 --- a/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_create.svelte +++ b/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_create.svelte @@ -44,7 +44,7 @@ response = await create_room(room); // go to room - if (response.status >= 300) + if (response.status >= 300 || response.error) show_error = response.error; else await change_room(response.room); diff --git a/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_protected.svelte b/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_protected.svelte index 9b614bf0..201582d2 100644 --- a/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_protected.svelte +++ b/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_protected.svelte @@ -32,7 +32,7 @@ response = await send_password(room); // go to room - if (response.status >= 300) + if (response.status >= 300 || response.error) show_error = response.error; else await change_room(response.room);