Skip to content

NotFoundException

reference
1 min readUpdated

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

mermaid
graph LR
  A[Controller or Service] -->|throws| B[NotFoundException]
  B --> C[HTTP Exception Handler]
  C --> D[HTTP 404 Not Found Response]
  D --> E[Client]

Usage

ts
import { 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 NotFoundException when 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 NotFoundException for 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)

  • RoutesResolverpackages/core/router/routes-resolver.ts:35
  • RecipesResolverintegration/graphql-code-first/src/recipes/recipes.resolver.ts:13
  • RecipesResolversample/23-graphql-code-first/src/recipes/recipes.resolver.ts:11
  • RecipesResolversample/33-graphql-mercurius/src/recipes/recipes.resolver.ts:16

Was this page helpful?

Download as PDF
NotFoundException — NestJS head-to-head