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
mermaidgraph 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
tsimport { 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
NotAcceptableExceptionwhen 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
UnsupportedMediaTypeExceptionfor unsupported request bodyContent-Typevalues. - Allow NestJS exception filters to serialize the exception unless the application requires a custom error-response format.
Was this page helpful?