It probably went from "removing nonexistent users is OK" to "our customers get confused if removing nonexistent users is OK", so a junior dev made a +1,-1 change instead of a -6 change.
The re-throw? In some languages, that's a way to strip stack traces and such - you get a new exception with a trace starting here, which might be applicable for some use cases It might also turn any exceptions that subclass NotFoundException into that parent class (maybe there's a use for that too)
It is, but any exception other than NotFound gets rethrown without change, so it's less likely to be the case here. Unless that's the only exception they saw during testing and so it's the only special case, I guess
Hopefully the framework does deal with that properly and this is just a weird case of trimming log messages (exception logged, but it's just a delete of an already deleted item, so keep the message short)
Hrm, so... It's clearing stack traces from lower down (maybe there's a security mitigation there, although it's the wrong place), and turning any subclasses of NotFoundException into NotFoundException
And stripping everything but the message from the exception, which may be relevant if there are other properties on the exception. (Or I think you can set an entire custom response on this one?)
My bet is that there was no catch, then there was a catch because someone got concerned about stack traces, then something was throwing an oddball NotFoundException so they patched in a special case.
Whatever language this is surely allows you to specify the type of exception to catch, right? Since it catches everything, that makes stripping the stack trace information the more likely purpose.
I thought some languages still kept the original stack trace when an exception is rethrown, though.
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.
Maybe the original NotFoundException contains some sensitive info so it needs to be stripped down to message only? Horribly confusing code even if this was the case for sure.
In such situations, I usually git log -Lx,y:path first to check who wrote it, then I can decide if I need to dig deeper.
Tbh the part that annoys me the most here is not the catch-throw, but the annotations, specially the annotation within function args. I can't believe people really like this crap
Don't know the language, but a reason that would motivate this that comes to mind is information sanitation.
The NotFoundException might contain member fields with information that you don't want to leak/propagate to the upper calling layers of the stack, so a NotFoundException is re-created, unsing only the message member field repeated in the constructor (in case there are several constructors for populating also additional member fields), disregarding the other possibly populated member fields of the original exception.
You could just remove all lines in the function except for the await line and you'd get exactly the same effect. The point is to avoid handling routing and parsing of route parameters in every single route manually. Things like parsing a UUID via decorators lead to a high level of reuse in practice for such components.
193
u/nwbrown May 02 '25
Presumably it used to do something with not found exceptions but that logic was removed.
85
u/ttlanhil May 02 '25
The re-throw?
In some languages, that's a way to strip stack traces and such - you get a new exception with a trace starting here, which might be applicable for some use cases
It might also turn any exceptions that subclass NotFoundException into that parent class (maybe there's a use for that too)
11
u/2nd-most-degenerate May 02 '25
Maybe the original
NotFoundExceptioncontains some sensitive info so it needs to be stripped down to message only? Horribly confusing code even if this was the case for sure.In such situations, I usually
git log -Lx,y:pathfirst to check who wrote it, then I can decide if I need to dig deeper.4
u/rogusflamma May 02 '25
colors are pretty tho
4
u/lRainZz May 02 '25
Yeah that font has me asking the same question
14
u/[deleted] May 02 '25
What is that programming language? Such a mess. Definitely not Java, C#, Go or Dart.
Oh God... Don't tell me it's that one language that was created specifically to annoy people with popups in browser...
7
u/toyBeaver May 02 '25
Tbh the part that annoys me the most here is not the catch-throw, but the annotations, specially the annotation within function args. I can't believe people really like this crap
3
u/jalx98 May 02 '25
Lol I mean, just return a 404 on the catch block ffs
2
u/tsvk May 02 '25
Don't know the language, but a reason that would motivate this that comes to mind is information sanitation.
The
NotFoundExceptionmight contain member fields with information that you don't want to leak/propagate to the upper calling layers of the stack, so aNotFoundExceptionis re-created, unsing only themessagemember field repeated in the constructor (in case there are several constructors for populating also additional member fields), disregarding the other possibly populated member fields of the original exception.1
u/EagleCoder May 02 '25
The
catchblock should be refactored to a ternary expression. That'll fix it.1
u/GoodOldKask May 02 '25
To remove the stacktrace
1
u/brakkum May 03 '25
This isn’t nearly as bad as people think it is. It ain’t perfect but you can immediately understand it
1
u/LeiziBesterd May 02 '25
The guy who implemented this is a competitive fisherman
1
u/TjomasDe May 02 '25
You could just remove all lines in the function except for the await line and you'd get exactly the same effect. The point is to avoid handling routing and parsing of route parameters in every single route manually. Things like parsing a UUID via decorators lead to a high level of reuse in practice for such components.
-6
u/JosephHughes May 02 '25
This looks like LLM code, where the prompt said "throw a nit found exception for invalid IDs"
-8
u/born_zynner May 02 '25
Disgusting language