NestJS · Senior · Question 71 of 75
Why should domain services throw domain errors rather than HttpExceptions?
- ✓Correct answer: Domain code is reused by HTTP, GraphQL, queues and CLI; mapping domain errors to transport errors at the edge (filters) keeps it independent
- 2Domain errors are faster
- 3
HttpExceptioncan't be thrown from services
Explanation
A UserNotFoundError becomes 404 in HTTP, NOT_FOUND in GraphQL, and a non-retryable failure in a queue.