Skip to content

NotAcceptableException

reference
1 min readUpdated

Kind: Class

Source: packages/common/exceptions/not-acceptable.exception.ts

Part of: Common

Defines an HTTP exception for Not Acceptable type errors.

NotAcceptableException represents an HTTP 406 Not Acceptable error. Use it when the server cannot provide a response that satisfies the client’s requested representation, such as an unsupported Accept header or response format.

Extends: HttpException

Diagram

mermaid
graph LR
  Client[Client request<br/>Accept header] --> Handler[Controller or service]
  Handler --> Validation{Requested format supported?}
  Validation -->|Yes| Response[Return compatible response]
  Validation -->|No| Exception[NotAcceptableException]
  Exception --> HttpResponse[HTTP 406 Not Acceptable]

Usage

ts
import { Controller, Get, Headers, NotAcceptableException } from '@nestjs/common';

@Controller('reports')
export class ReportsController {
  @Get()
  getReport(@Headers('accept') accept?: string) {
    if (accept && !accept.includes('application/json')) {
      throw new NotAcceptableException(
        'This endpoint only supports application/json responses.',
      );
    }

    return {
      id: 'report-123',
      format: 'json',
    };
  }
}

AI Coding Instructions

  • Throw NotAcceptableException when content negotiation fails and no acceptable response representation can be returned.
  • Prefer a clear, client-safe error message that identifies the unsupported requested format or media type.
  • Use this exception for HTTP 406 scenarios; use UnsupportedMediaTypeException for unsupported request body Content-Type values.
  • Allow NestJS exception filters to serialize the exception unless the application requires a custom error-response format.

Was this page helpful?

Download as PDF
NotAcceptableException — NestJS head-to-head