CodePark/ questions / Error handling & API boundaries
START PRACTICINGPRACTICE
Backend question · Software design & patterns › Error handling & API boundaries

What is the main problem with this code?

async function getUser(id: string) {
  try {
    return await repo.findById(id);
  } catch {
    return null;
  }
}
It swallows every error, so a database outage looks the same as a missing user

The code catches every error and returns null, so it hides real failures. If the database is down, callers think the user is missing. They may answer 404 or create a duplicate user. Also, nothing is logged, so nobody sees the outage.

"The
catch block needs an (err) parameter to compile" is wrong. A catch without (err) is called optional catch binding, and it is valid modern JavaScript.