Error Handling
ELI5 — The Vibe Check
Error handling is the art of planning for things to go wrong and dealing with them gracefully instead of letting everything catch fire. Good error handling means your app shows a friendly message instead of crashing. Bad error handling means users see raw error dumps or, worse, a white blank screen.
Real Talk
Error handling encompasses all strategies for detecting, catching, and recovering from errors at runtime. It includes try/catch blocks, error boundary components (React), promise rejection handlers, global error handlers (process.on('uncaughtException')), input validation, and meaningful error messages for debugging. Good error handling distinguishes between expected errors (validation failures) and unexpected errors (bugs).
Show Me The Code
// Layered error handling in an Express API:
app.get('/user/:id', async (req, res) => {
try {
const user = await db.findUser(req.params.id);
if (!user) return res.status(404).json({ error: 'Not found' });
res.json(user);
} catch (err) {
console.error(err); // log for devs
res.status(500).json({ error: 'Internal server error' }); // safe for users
}
});
When You'll Hear This
"The error handling needs work — it's showing raw stack traces to users." / "Add proper error handling before we ship this."
Related Terms
Bug
A bug is anything in your code that makes it behave wrong.
Exception
An exception is a special kind of error that 'throws' itself up through your code like a hot potato, looking for someone to catch it.
Logging
Logging is writing a diary for your program.
Try/Catch
Try/catch is your safety net. You put risky code in the 'try' box, and if it blows up, the 'catch' box catches the explosion and handles it gracefully inst...
Validation
Validation is your backend's bouncer. Before any data gets into the database, the bouncer checks it: 'Is this email actually an email?