Skip to content

ConflictException

reference
1 min readUpdated

Kind: Class

Source: packages/common/exceptions/conflict.exception.ts

Part of: Common

Defines an HTTP exception for Conflict type errors.

ConflictException represents an HTTP 409 Conflict error. Use it when a request cannot be completed because it conflicts with the current state of a resource, such as attempting to create a record that already exists.

Extends: HttpException

Diagram

mermaid
graph LR
  A[Application logic] -->|Detects conflicting state| B[ConflictException]
  B --> C[HTTP exception handler]
  C -->|HTTP 409 Conflict response| D[Client]

Usage

ts
import { ConflictException } from '@nestjs/common';

async function createUser(email: string) {
  const existingUser = await usersRepository.findByEmail(email);

  if (existingUser) {
    throw new ConflictException(
      `A user with email "${email}" already exists.`,
    );
  }

  return usersRepository.create({ email });
}

AI Coding Instructions

  • Throw ConflictException only for resource-state conflicts, such as duplicate unique fields or incompatible concurrent updates.
  • Prefer a clear, client-safe error message that explains which conflict occurred without exposing sensitive data.
  • Do not use this exception for validation failures; use a bad-request or validation-specific exception instead.
  • Let the framework’s global exception handling convert the exception into the standard HTTP 409 response.

Was this page helpful?

Download as PDF
ConflictException — NestJS head-to-head