# Nestjs Exception Filters

> NestJS exception handling and filters best practices. Use when handling errors in NestJS — throwing HTTP exceptions, custom exception classes, or global exception filters. Triggers on HttpException, BadRequestException, @Catch, ExceptionFilter, or error response shape.

- **Type:** Skill
- **Install:** `agentstack add skill-dkmqflx-nestjs-best-practices-plugin-nestjs-exception-filters`
- **Verified:** Yes — security-reviewed for prompt injection and unsafe behavior
- **Seller:** [dkmqflx](https://agentstack.voostack.com/s/dkmqflx)
- **Installs:** 0
- **Category:** [Agent Skills](https://agentstack.voostack.com/c/agent-skills)
- **Latest version:** 0.1.0
- **License:** MIT
- **Upstream author:** [dkmqflx](https://github.com/dkmqflx)
- **Source:** https://github.com/dkmqflx/nestjs-best-practices-plugin/tree/main/plugins/nestjs-best-practices/skills/nestjs-exception-filters

## Install

```sh
agentstack add skill-dkmqflx-nestjs-best-practices-plugin-nestjs-exception-filters
```

Requires the [AgentStack CLI](https://agentstack.voostack.com/docs/cli). Works with Claude Code, Cursor, and any MCP-compatible agent.

## About

# NestJS Exception Handling & Filters

NestJS ships a built-in exceptions layer that catches unhandled exceptions and turns them into a sensible HTTP response. Lean on it: throw exceptions from your business logic and let filters shape the response. These rules cover throwing the right exception, modeling domain errors, and centralizing error formatting with global filters — for NestJS v10/v11.

## When to Apply

- Throwing errors from controllers, services, guards, pipes, or interceptors.
- Designing custom/domain exception classes.
- Building a global exception filter (`APP_FILTER`) or a catch-all filter.
- Standardizing the JSON error body returned to clients.
- Reviewing code that manually builds error responses on `res`.

## Rules

| Rule | Impact | Summary |
|------|--------|---------|
| [use-builtin-http-exceptions](rules/use-builtin-http-exceptions.md) | HIGH | Throw `BadRequestException`/`NotFoundException` etc. instead of generic `Error`. |
| [domain-exception-classes](rules/domain-exception-classes.md) | MEDIUM | Model domain errors as custom classes extending `HttpException`. |
| [global-exception-filter](rules/global-exception-filter.md) | HIGH | Register a global filter via `APP_FILTER` for consistent formatting. |
| [catch-all-unknown-errors](rules/catch-all-unknown-errors.md) | HIGH | Use a `@Catch()` catch-all filter to map unknown errors to 500. |
| [dont-leak-internal-details](rules/dont-leak-internal-details.md) | CRITICAL | Never expose stack traces, SQL, or secrets; log them server-side only. |
| [consistent-error-shape](rules/consistent-error-shape.md) | MEDIUM | Return a uniform JSON error body across the API. |
| [throw-dont-return-errors](rules/throw-dont-return-errors.md) | HIGH | Let exceptions propagate to filters; don't hand-craft `res` in handlers. |

## How to Use

1. In handlers/services, throw built-in or domain exceptions — never construct the HTTP response by hand.
2. Add one global catch-all filter (`APP_FILTER`) that formats every error into your standard shape and maps unknown errors to 500.
3. In that filter, log full internals server-side and return only safe, client-facing fields.
4. Skim the rule files above for the incorrect/correct examples before writing or reviewing error-handling code.

## Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

- **Author:** [dkmqflx](https://github.com/dkmqflx)
- **Source:** [dkmqflx/nestjs-best-practices-plugin](https://github.com/dkmqflx/nestjs-best-practices-plugin)
- **License:** MIT

Install and usage instructions live in the source repository linked above.

## Pricing

- **Free** — Free

## Security capabilities

Automated source analysis of v0.1.0 — what this tool can access:

- **Network access:** no
- **Filesystem access:** no
- **Shell / process execution:** no
- **Environment & secrets:** no
- **Dynamic code execution:** no

*"Yes" means the capability is present in the source — more access means more to trust, not that it is unsafe.*


## Versions

- **0.1.0** — security scan: passed — Imported from the upstream source.

## Links

- Listing page: https://agentstack.voostack.com/l/skill-dkmqflx-nestjs-best-practices-plugin-nestjs-exception-filters
- Seller: https://agentstack.voostack.com/s/dkmqflx
- Browse the marketplace: https://agentstack.voostack.com/browse

---
Listed on AgentStack — the marketplace for AI agent skills and MCP servers. Every listing is security-reviewed. Creators keep 70%.
