Kind: Class
Source: packages/common/exceptions/unsupported-media-type.exception.ts
Part of: Common
Defines an HTTP exception for Unsupported Media Type type errors.
UnsupportedMediaTypeException represents an HTTP 415 error, indicating that the server cannot process a request because its Content-Type is unsupported. It is typically thrown by controllers, guards, or middleware during request validation so NestJS can return a standardized HTTP error response.
Extends: HttpException
Diagram
mermaidgraph LR A[Client request] --> B[Content-Type validation] B -->|Supported media type| C[Controller handler] B -->|Unsupported media type| D[UnsupportedMediaTypeException] D --> E[HTTP 415 response]
Usage
tsimport {
Controller,
Post,
Headers,
UnsupportedMediaTypeException,
} from '@nestjs/common';
@Controller('uploads')
export class UploadController {
@Post()
upload(@Headers('content-type') contentType?: string) {
if (!contentType?.startsWith('image/png')) {
throw new UnsupportedMediaTypeException(
'Only image/png uploads are supported.',
);
}
return { message: 'Upload accepted' };
}
}
AI Coding Instructions
- Throw this exception when validation fails specifically because the request media type or
Content-Typeheader is unsupported. - Prefer HTTP 415 over
BadRequestExceptionwhen the request body format is valid but its declared media type is not accepted. - Validate media types with
startsWith()when parameters such ascharsetor multipart boundaries may be present. - Provide a clear error message describing the accepted media types for API consumers.
- Use NestJS exception filters or the default exception handling pipeline rather than manually constructing HTTP 415 responses.
Was this page helpful?