Skip to content

PreconditionFailedException

reference
1 min readUpdated

Kind: Class

Source: packages/common/exceptions/precondition-failed.exception.ts

Part of: Common

Defines an HTTP exception for Precondition Failed type errors.

PreconditionFailedException represents an HTTP 412 Precondition Failed error. Use it when a request cannot be completed because one or more client-supplied preconditions—such as an ETag, version, or conditional header—do not match the current resource state.

Extends: HttpException

Diagram

mermaid
graph LR
  Client[Client request with precondition] --> Handler[Application handler]
  Handler --> Check{Precondition valid?}
  Check -->|Yes| Update[Process request]
  Check -->|No| Exception[PreconditionFailedException]
  Exception --> Response[HTTP 412 Precondition Failed response]

Usage

ts
import { PreconditionFailedException } from '@nestjs/common';

async function updateDocument(
  id: string,
  expectedVersion: number,
  content: string,
) {
  const document = await documentsService.findById(id);

  if (document.version !== expectedVersion) {
    throw new PreconditionFailedException(
      'The document has changed since it was last retrieved.',
    );
  }

  return documentsService.update(id, content);
}

AI Coding Instructions

  • Throw this exception only when a request's explicit precondition fails, such as an ETag, If-Match header, or optimistic-lock version check.
  • Prefer a clear, client-actionable message that explains the resource must be refreshed before retrying.
  • Do not use this exception for validation errors; use the appropriate bad-request or validation exception instead.
  • Allow the framework exception filter to serialize the exception into the standard HTTP 412 response shape.

Used by

1 reference from 1 file. Each is a place in this repository where the symbol is actually used — go read one rather than trusting an example.

Imported by (1)

  • DurableServiceintegration/scopes/src/durable/durable.service.ts:10

Was this page helpful?

Download as PDF
PreconditionFailedException — NestJS head-to-head