From 79f11edb65d80faedb539bd438b476c2973c2273 Mon Sep 17 00:00:00 2001 From: "T. Narantuya" Date: Sat, 6 Sep 2025 14:23:51 +0900 Subject: [PATCH] Enhance authentication and user service error handling - Added HttpCode decorator to the checkPasswordNeeded endpoint for consistent response status. - Updated email validation in UsersService to throw BadRequestException for improved error handling. - Introduced NotFoundException in WhmcsConnectionService for better error messaging when clients are not found. --- apps/bff/src/auth/auth.controller.ts | 3 ++- apps/bff/src/users/users.service.ts | 6 ++++-- .../whmcs/services/whmcs-connection.service.ts | 17 ++++++++++++++++- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/apps/bff/src/auth/auth.controller.ts b/apps/bff/src/auth/auth.controller.ts index 9f8dc1b6..20c8009b 100644 --- a/apps/bff/src/auth/auth.controller.ts +++ b/apps/bff/src/auth/auth.controller.ts @@ -1,4 +1,4 @@ -import { Controller, Post, Body, UseGuards, Get, Req } from "@nestjs/common"; +import { Controller, Post, Body, UseGuards, Get, Req, HttpCode } from "@nestjs/common"; import type { Request } from "express"; import { Throttle } from "@nestjs/throttler"; import { AuthService } from "./auth.service"; @@ -116,6 +116,7 @@ export class AuthController { @Public() @Post("check-password-needed") + @HttpCode(200) @ApiOperation({ summary: "Check if user needs to set password" }) @ApiResponse({ status: 200, description: "Password status checked" }) async checkPasswordNeeded(@Body() { email }: { email: string }) { diff --git a/apps/bff/src/users/users.service.ts b/apps/bff/src/users/users.service.ts index b3aefd1a..d6a5c494 100644 --- a/apps/bff/src/users/users.service.ts +++ b/apps/bff/src/users/users.service.ts @@ -121,8 +121,10 @@ export class UsersService { private validateEmail(email: string): string { const trimmed = email?.toLowerCase().trim(); - if (!trimmed) throw new Error("Email is required"); - if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(trimmed)) throw new Error("Invalid email format"); + if (!trimmed) throw new BadRequestException("Email is required"); + if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(trimmed)) { + throw new BadRequestException("Invalid email format"); + } return trimmed; } diff --git a/apps/bff/src/vendors/whmcs/services/whmcs-connection.service.ts b/apps/bff/src/vendors/whmcs/services/whmcs-connection.service.ts index ac9959f0..4d33f8bb 100644 --- a/apps/bff/src/vendors/whmcs/services/whmcs-connection.service.ts +++ b/apps/bff/src/vendors/whmcs/services/whmcs-connection.service.ts @@ -1,6 +1,6 @@ import { getErrorMessage } from "../../../common/utils/error.util"; import { Logger } from "nestjs-pino"; -import { Injectable, Inject } from "@nestjs/common"; +import { Injectable, Inject, NotFoundException } from "@nestjs/common"; import { ConfigService } from "@nestjs/config"; import { WhmcsApiResponse, @@ -171,6 +171,21 @@ export class WhmcsConnectionService { params: this.sanitizeLogParams(params), authModeTried: useAdminAuth ? "admin" : "api_credentials", }); + // Normalize common, expected error responses to domain exceptions + if ( + action === "GetClientsDetails" && + typeof errorResponse.message === "string" && + errorResponse.message.toLowerCase().includes("client not found") + ) { + const byEmail = typeof (params as any).email === "string" ? (params as any).email : undefined; + if (byEmail) { + throw new NotFoundException(`Client with email ${byEmail} not found`); + } + const byId = (params as any).clientid; + throw new NotFoundException( + `Client ${typeof byId === "string" || typeof byId === "number" ? byId : ""} not found` + ); + } throw new Error(`WHMCS API Error: ${errorResponse.message}`); }