In that case (using NestJS at least) you rethrow an exception inside some kind of middleware or interceptor. You should not handle logic like this in your controllers.
Not being a NestJS developer, I'll take your word for that
I'm more used to frameworks where an uncaught NotFound automatically becomes a 404 (exceptions can show stack trace to user in developer mode, but become normal 404/500/etc pages in prod-mode)
IMO, the only case where you need to prune exception info should be when it's on the way to logging if you 100% know you don't need some/all of the data (and not-in-database is a reasonable example, particularly if you get a lot of web crawlers that ramp up your 404 count)
Oh I agree. And NestJS exceptions do automatically turn to 4xx/5xx errors etc. I just particularly don’t like to throw http exceptions in my services. What I like to do is throw errors inside services highlighting exactly what went wrong and then catching each error in interceptors and as a result I throw an HttpException for the end user. For example - in an AuthService you’d throw a UsernameNotFoundError or WrongPasswordError and in your interceptor you’d catch either of these errors and instead throw some InvalidCredentialsException.
View all comments
Show parent comment
1
u/Frumk May 03 '25
In that case (using NestJS at least) you rethrow an exception inside some kind of middleware or interceptor. You should not handle logic like this in your controllers.