Skip to content

ImATeapotException

reference
1 min readUpdated

Kind: Class

Source: packages/common/exceptions/im-a-teapot.exception.ts

Part of: Common

Defines an HTTP exception for ImATeapotException type errors.

Any attempt to brew coffee with a teapot should result in the error code "418 I'm a teapot". The resulting entity body MAY be short and stout.

ImATeapotException represents the HTTP 418 I'm a Teapot response defined for requests that cannot be fulfilled because a teapot was asked to brew coffee. It extends the framework’s HTTP exception system so controllers, guards, or services can return a consistent status code and response body for this intentionally humorous but standardized error case.

Extends: HttpException

Diagram

mermaid
graph LR
  A[Incoming HTTP Request] --> B{Request asks teapot<br/>to brew coffee?}
  B -- Yes --> C[Throw ImATeapotException]
  C --> D[HTTP Exception Handler]
  D --> E[418 I'm a Teapot Response]
  B -- No --> F[Continue normal processing]

Usage

ts
import { Controller, Get } from '@nestjs/common';
import { ImATeapotException } from '@nestjs/common';

@Controller('coffee')
export class CoffeeController {
  @Get('brew')
  brewCoffee() {
    const appliance = 'teapot';

    if (appliance === 'teapot') {
      throw new ImATeapotException(
        'A teapot cannot brew coffee. The response body may be short and stout.',
      );
    }

    return { status: 'brewing' };
  }
}

AI Coding Instructions

  • Throw ImATeapotException only when the intended HTTP response is 418 I'm a Teapot.
  • Provide a concise response message or body when useful; callers may rely on the exception’s standard HTTP status code.
  • Prefer framework exception handling over manually constructing a Response with status 418.
  • Use this exception in controllers or service-layer validation paths where the request is semantically inappropriate, not for general client errors.

Was this page helpful?

Download as PDF
ImATeapotException — NestJS head-to-head