Kind: Class
Source: packages/common/exceptions/method-not-allowed.exception.ts
Part of: Common
Defines an HTTP exception for Method Not Allowed type errors.
MethodNotAllowedException represents an HTTP 405 error when a request uses a method that is not supported by a route or resource. It integrates with the common exception handling system so unsupported methods can be converted into a consistent HTTP error response.
Extends: HttpException
Diagram
mermaidgraph LR Client[HTTP Client] --> Request[Request with unsupported method] Request --> Route[Route/Controller] Route --> Exception[MethodNotAllowedException] Exception --> Handler[Global Exception Handler] Handler --> Response[HTTP 405 Method Not Allowed]
Usage
tsimport { MethodNotAllowedException } from '@nestjs/common';
function updateUser(requestMethod: string) {
if (requestMethod !== 'PATCH') {
throw new MethodNotAllowedException(
`Method ${requestMethod} is not allowed for this resource.`,
);
}
return { updated: true };
}
AI Coding Instructions
- Throw
MethodNotAllowedExceptionwhen a route exists but does not support the incoming HTTP method. - Prefer framework routing configuration for normal method enforcement; use this exception for explicit runtime validation or custom dispatching.
- Include a clear error message when the allowed method or resource context helps API consumers diagnose the issue.
- Do not use this exception for missing routes; use a not-found exception when no matching resource or endpoint exists.
Was this page helpful?