Tech TutorBangla Web Documentation
Portfolio
HomeHTMLCSSTailwind CSSJavaScriptTypeScriptReactNext.jsNode.jsExpressSocket.IOExpoWordPressPostgreSQLPrismaPostgresNestJSMongoDBDockerTanStackTanStack TableRTK QueryGitGitHub
  • NestJS Documentation21টি অধ্যায়
প্রযুক্তি নির্বাচন
HomeHTMLCSSTailwind CSSJavaScriptTypeScriptReactNext.jsNode.jsExpressSocket.IOExpoWordPressPostgreSQLPrismaPostgresNestJSMongoDBDockerTanStackTanStack TableRTK QueryGitGitHub
  • Introduction
  • Installation
  • Modules
  • Controllers
  • Providers & DI
  • Pipes
  • Guards
  • Interceptors
  • Exception Handling
  • Middleware
  • Database
  • Authentication
  • Config & Env
  • File Upload
  • WebSockets
  • Microservices
  • Swagger
  • Testing
  • Best Practices
  • Project
  • Interview
  1. Docs
  2. NestJS
  3. NestJS Exception Handling
NestJSIntermediate

NestJS Exception Handling

Built-in exceptions, custom exception, exception filter — Error handling বাংলায়।

10টি অংশবাংলা ভাষায়

NestJS built-in exceptions দেয় (NotFoundException, BadRequestException ইত্যাদি) এবং Exception Filter দিয়ে সম্পূর্ণ control করা যায়।

Built-in Exceptions

import {
  BadRequestException,
  NotFoundException,
  UnauthorizedException,
  ForbiddenException,
  ConflictException,
  HttpException,
  HttpStatus,
} from "@nestjs/common";

@Get(":id")
findOne(id: number) {
  if (!this.userExists(id)) {
    throw new NotFoundException("User পাওয়া যায়নি");
  }
}

create(data) {
  if (data.emailExists) {
    throw new ConflictException("This email already exists");
  }
  throw new HttpException(
    { success: false, message: "Custom message" },
    HttpStatus.BAD_REQUEST,
  );
}

Custom Exception

src/common/exceptions/app.exception.tstypescript
typescript
import { HttpException, HttpStatus } from "@nestjs/common";

export class AppException extends HttpException {
  constructor(message: string, status: HttpStatus) {
    super(
      { success: false, message, timestamp: new Date().toISOString() },
      status,
    );
  }
}

// ব্যবহার
throw new AppException("Payment failed", HttpStatus.PAYMENT_REQUIRED);

Exception Filter

src/common/all-exceptions.filter.tstypescript
typescript
import {
  ArgumentsHost,
  Catch,
  ExceptionFilter,
  HttpException,
  HttpStatus,
} from "@nestjs/common";
import { Request, Response } from "express";

@Catch()
export class AllExceptionsFilter implements ExceptionFilter {
  catch(exception: unknown, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse<Response>();
    const request = ctx.getRequest<Request>();

    const status =
      exception instanceof HttpException
        ? exception.getStatus()
        : HttpStatus.INTERNAL_SERVER_ERROR;

    const message =
      exception instanceof HttpException
        ? (exception.getResponse() as any).message ?? exception.message
        : "Internal Server Error";

    response.status(status).json({
      success: false,
      statusCode: status,
      message,
      path: request.url,
      timestamp: new Date().toISOString(),
    });
  }
}

Filter Apply

// controller level
@UseFilters(new AllExceptionsFilter())
export class UsersController {}

// global (main.ts)
import { AllExceptionsFilter } from "./common/all-exceptions.filter";
const app = await NestFactory.create(AppModule);
app.useGlobalFilters(new AllExceptionsFilter());
Best Practice

Global exception filter দিয়ে consistent error format রাখুন — frontend-এ parse করা সহজ হয়। বিশেষ log-এর জন্য @Catch(PrismaClientExceptionFilter) আলাদা filter ব্যবহার করুন।

শেষ প্যারায় নিজে practice করুন — কোড কপি করে নিজের এডিটরে চালিয়ে দেখুন।
আগেরNestJS InterceptorsপরেরNestJS Middleware
এই পেজে
Tech Tutor

বাংলায় ওয়েব ডেভেলপমেন্ট ডকুমেন্টেশন শিখুন — ধাপে ধাপে।

Homeসব ডকুমেন্টেশনQuick Startআমার নোটস

© 2026 Tech Tutor. বাংলায় শিখুন, বাংলায় গড়ুন।

mdanamolhasan1998@gmail.com