From 94c609cc4325c9217de69c6eb8bb0238ed55b2c1 Mon Sep 17 00:00:00 2001 From: Mustafa Shoukat <162743520+Mustafa-Shoukat1@users.noreply.github.com> Date: Tue, 19 May 2026 23:31:23 +0600 Subject: [PATCH] fix: replace generic Error with proper NestJS HTTP exceptions in controllers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Multiple controllers (channel, contact, group, label) throw generic JavaScript `Error` instead of NestJS HTTP exceptions. This causes 500 Internal Server Error responses instead of the correct 400/404 status codes documented in Swagger. Changes: - channel.controller.ts: Use BadRequestException for "session not started" and NotFoundException for "channel not found" - contact.controller.ts: Use BadRequestException for "session not started" and NotFoundException for "contact not found" - group.controller.ts: Use BadRequestException for "session not started" and NotFoundException for "group not found" - label.controller.ts: Use BadRequestException for "session not started" and NotFoundException for "label not found" - dashboard/src/services/api.ts: Add missing 'failed' status to Session type to match backend SessionStatus enum This also relates to issue #100 — when a session disconnects from the phone, the engine is destroyed and subsequent API calls return 500 instead of a proper 400 Bad Request. --- dashboard/src/services/api.ts | 2 +- src/modules/channel/channel.controller.ts | 6 +++--- src/modules/contact/contact.controller.ts | 16 ++++++++-------- src/modules/group/group.controller.ts | 6 +++--- src/modules/label/label.controller.ts | 6 +++--- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/dashboard/src/services/api.ts b/dashboard/src/services/api.ts index 80972ae..31ccfc5 100644 --- a/dashboard/src/services/api.ts +++ b/dashboard/src/services/api.ts @@ -10,7 +10,7 @@ const API_BASE_URL = '/api'; export interface Session { id: string; name: string; - status: 'created' | 'idle' | 'initializing' | 'connecting' | 'qr_ready' | 'ready' | 'disconnected'; + status: 'created' | 'idle' | 'initializing' | 'connecting' | 'qr_ready' | 'ready' | 'disconnected' | 'failed'; phone?: string; pushName?: string; lastActive?: string; diff --git a/src/modules/channel/channel.controller.ts b/src/modules/channel/channel.controller.ts index c4c110d..053c98f 100644 --- a/src/modules/channel/channel.controller.ts +++ b/src/modules/channel/channel.controller.ts @@ -1,4 +1,4 @@ -import { Controller, Get, Post, Delete, Param, Body, Query } from '@nestjs/common'; +import { Controller, Get, Post, Delete, Param, Body, Query, BadRequestException, NotFoundException } from '@nestjs/common'; import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiBody, ApiQuery } from '@nestjs/swagger'; import { SessionService } from '../session/session.service'; @@ -10,7 +10,7 @@ export class ChannelController { private getEngine(sessionId: string) { const engine = this.sessionService.getEngine(sessionId); if (!engine) { - throw new Error('Session is not started'); + throw new BadRequestException('Session is not started'); } return engine; } @@ -41,7 +41,7 @@ export class ChannelController { const engine = this.getEngine(sessionId); const channel = await engine.getChannelById(channelId); if (!channel) { - throw new Error(`Channel ${channelId} not found`); + throw new NotFoundException(`Channel ${channelId} not found`); } return channel; } diff --git a/src/modules/contact/contact.controller.ts b/src/modules/contact/contact.controller.ts index 22310f2..72db80d 100644 --- a/src/modules/contact/contact.controller.ts +++ b/src/modules/contact/contact.controller.ts @@ -1,4 +1,4 @@ -import { Controller, Get, Post, Delete, Param, HttpCode, HttpStatus } from '@nestjs/common'; +import { Controller, Get, Post, Delete, Param, HttpCode, HttpStatus, BadRequestException, NotFoundException } from '@nestjs/common'; import { ApiTags, ApiOperation, ApiResponse, ApiParam } from '@nestjs/swagger'; import { SessionService } from '../session/session.service'; @@ -19,7 +19,7 @@ export class ContactController { async findAll(@Param('sessionId') sessionId: string) { const engine = this.sessionService.getEngine(sessionId); if (!engine) { - throw new Error('Session is not started'); + throw new BadRequestException('Session is not started'); } return engine.getContacts(); } @@ -36,11 +36,11 @@ export class ContactController { async findOne(@Param('sessionId') sessionId: string, @Param('contactId') contactId: string) { const engine = this.sessionService.getEngine(sessionId); if (!engine) { - throw new Error('Session is not started'); + throw new BadRequestException('Session is not started'); } const contact = await engine.getContactById(contactId); if (!contact) { - throw new Error(`Contact ${contactId} not found`); + throw new NotFoundException(`Contact ${contactId} not found`); } return contact; } @@ -56,7 +56,7 @@ export class ContactController { async checkNumber(@Param('sessionId') sessionId: string, @Param('number') number: string) { const engine = this.sessionService.getEngine(sessionId); if (!engine) { - throw new Error('Session is not started'); + throw new BadRequestException('Session is not started'); } const exists = await engine.checkNumberExists(number); return { @@ -79,7 +79,7 @@ export class ContactController { async getProfilePicture(@Param('sessionId') sessionId: string, @Param('contactId') contactId: string) { const engine = this.sessionService.getEngine(sessionId); if (!engine) { - throw new Error('Session is not started'); + throw new BadRequestException('Session is not started'); } const url = await engine.getProfilePicture(contactId); return { url }; @@ -97,7 +97,7 @@ export class ContactController { async blockContact(@Param('sessionId') sessionId: string, @Param('contactId') contactId: string) { const engine = this.sessionService.getEngine(sessionId); if (!engine) { - throw new Error('Session is not started'); + throw new BadRequestException('Session is not started'); } await engine.blockContact(contactId); return { success: true, message: 'Contact blocked' }; @@ -114,7 +114,7 @@ export class ContactController { async unblockContact(@Param('sessionId') sessionId: string, @Param('contactId') contactId: string) { const engine = this.sessionService.getEngine(sessionId); if (!engine) { - throw new Error('Session is not started'); + throw new BadRequestException('Session is not started'); } await engine.unblockContact(contactId); return { success: true, message: 'Contact unblocked' }; diff --git a/src/modules/group/group.controller.ts b/src/modules/group/group.controller.ts index 69a063d..65043de 100644 --- a/src/modules/group/group.controller.ts +++ b/src/modules/group/group.controller.ts @@ -1,4 +1,4 @@ -import { Controller, Get, Post, Put, Delete, Param, Body, HttpCode, HttpStatus } from '@nestjs/common'; +import { Controller, Get, Post, Put, Delete, Param, Body, HttpCode, HttpStatus, BadRequestException, NotFoundException } from '@nestjs/common'; import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiBody } from '@nestjs/swagger'; import { SessionService } from '../session/session.service'; @@ -44,7 +44,7 @@ export class GroupController { const engine = this.getEngine(sessionId); const group = await engine.getGroupInfo(groupId); if (!group) { - throw new Error(`Group ${groupId} not found`); + throw new NotFoundException(`Group ${groupId} not found`); } return group; } @@ -205,7 +205,7 @@ export class GroupController { private getEngine(sessionId: string) { const engine = this.sessionService.getEngine(sessionId); if (!engine) { - throw new Error('Session is not started'); + throw new BadRequestException('Session is not started'); } return engine; } diff --git a/src/modules/label/label.controller.ts b/src/modules/label/label.controller.ts index b6baead..d109dff 100644 --- a/src/modules/label/label.controller.ts +++ b/src/modules/label/label.controller.ts @@ -1,4 +1,4 @@ -import { Controller, Get, Post, Delete, Param, Body } from '@nestjs/common'; +import { Controller, Get, Post, Delete, Param, Body, BadRequestException, NotFoundException } from '@nestjs/common'; import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiBody } from '@nestjs/swagger'; import { SessionService } from '../session/session.service'; @@ -10,7 +10,7 @@ export class LabelController { private getEngine(sessionId: string) { const engine = this.sessionService.getEngine(sessionId); if (!engine) { - throw new Error('Session is not started'); + throw new BadRequestException('Session is not started'); } return engine; } @@ -42,7 +42,7 @@ export class LabelController { const engine = this.getEngine(sessionId); const label = await engine.getLabelById(labelId); if (!label) { - throw new Error(`Label ${labelId} not found`); + throw new NotFoundException(`Label ${labelId} not found`); } return label; }