Kind: Class
Source: packages/common/exceptions/not-found.exception.ts
Part of: Common
Defines an HTTP exception for Not Found type errors.
NotFoundException represents an HTTP 404 error when a requested resource cannot be found. It extends the application's HTTP exception system, providing a consistent way for controllers and services to signal missing entities to the framework's exception handling layer.
Extends: HttpException
Diagram
mermaidgraph LR A[Controller or Service] -->|throws| B[NotFoundException] B --> C[HTTP Exception Handler] C --> D[HTTP 404 Not Found Response] D --> E[Client]
Usage
tsimport { NotFoundException } from '@nestjs/common';
async function findUserById(id: string) {
const user = await usersRepository.findById(id);
if (!user) {
throw new NotFoundException(`User with ID "${id}" was not found`);
}
return user;
}
AI Coding Instructions
- Throw
NotFoundExceptionwhen a requested resource does not exist or cannot be resolved by its identifier. - Include a clear, safe error message that identifies the missing resource without exposing sensitive implementation details.
- Use this exception instead of manually constructing
{ statusCode: 404 }response objects; the framework exception filter formats the HTTP response. - Do not use
NotFoundExceptionfor validation failures, authorization issues, or unexpected persistence errors; use the appropriate exception type instead.
Used by
4 references from 4 files. Each is a place in this repository where the symbol is actually used — go read one rather than trusting an example.
Imported by (4)
RoutesResolver—packages/core/router/routes-resolver.ts:35RecipesResolver—integration/graphql-code-first/src/recipes/recipes.resolver.ts:13RecipesResolver—sample/23-graphql-code-first/src/recipes/recipes.resolver.ts:11RecipesResolver—sample/33-graphql-mercurius/src/recipes/recipes.resolver.ts:16
Was this page helpful?