Invental/Review lessons/Code that silently does the wrong thing

Code that silently does the wrong thing

Map not-found to 404 around one lookup

Translate a database “not found” into a 404 only around the one query whose absence means “not found”. If the catch wraps a whole flow, a missing row in a later query gets reported as the wrong error.

01 · Before and afterThe pattern

TypeScript
// before: catch-all around the whole flow
try { await cancelBooking(input) } catch (e) { if (isNotFound(e)) throw http404("Booking not found") ; throw e }
// after: catch only the lookup that defines "not found"
let booking;
try { booking = await getBooking(id) } catch (e) { if (isNotFound(e)) throw http404("Booking not found"); throw e }
await cancelBooking(booking); // later not-found errors keep their real meaning

02 · Run it on your appThe check

Does each 404 in your API come from one specific lookup, not from a catch around the whole request?

If the honest answer is no or not sure, this is worth a look before it reaches production.

Want this checked on your code?

Tell us what you are building and what worries you. We match a senior reviewer to your code and confirm scope before anything starts.

— Get in touch
hi@invental.co ↗
— Or
— What to include

A link or short description of the code or system, what you want checked, your stack, and when you need the answer. No repository access is needed until scope is agreed.

— Or leave a note
We reply within one business day.