Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix: replace generic Error with proper NestJS HTTP exceptions in controllers (#102)

Mustafa Shoukat8e5770a423ac
diff --git a/dashboard/src/services/api.ts b/dashboard/src/services/api.ts
index 80972ae31..31ccfc57c 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 c4c110d16..053c98f03 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 22310f294..72db80d11 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 69a063dc9..65043de77 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 b6baead5f..d109dff2a 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;
}