| name | handling-nestjs-exceptions |
| description | Maps internal AppError hierarchy to HTTP responses through a single global exception filter and a Result-to-HttpException helper. Use when adding new error types, fixing inconsistent error responses, or removing scattered try/catch in controllers. |
| license | MIT |
Handling NestJS Exceptions
When to use
- Adding new error types
- Fixing inconsistent error responses
- Removing scattered try/catch in controllers
Core rules
- Single global exception filter catches all
AppError subclasses
Result<T,E> mapped to HTTP via helper, not scattered try/catch
- All errors extend
AppError with statusCode
- No
try/catch in controllers; let exceptions bubble to filter
Reference shape (TypeScript)
Global Exception Filter
@Catch(AppError)
export class AppExceptionFilter implements ExceptionFilter {
catch(exception: AppError, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const res = ctx.getResponse<Response>();
res.status(exception.statusCode || 400).json({
success: false,
error: { code: exception.code, message: exception.message },
});
}
}
Result to HttpException Helper
export function resultToHttp<T>(result: Result<T, AppError>): T {
if (!result.success) throw result.error;
return result.value;
}
Examples — Do
@Get(':id')
async findOne(@Param('id') id: string): Promise<ApiResponse<User>> {
const result = await this.useCase.execute(id);
return toApiResponse(result);
}
Examples — Don't
try { ... } catch (e) { return res.status(400).json({ error: e.message }); }
Checklist
See reference/exception-patterns.md for full patterns.