Error Handling in Node.js
Error Types
1. Operational Errors
Runtime problems (network issues, database errors)
2. Programmer Errors
Bugs in code (undefined variables, type errors)
Try-Catch
try {
const data = JSON.parse(invalidJSON);
} catch (err) {
console.error('Parse error:', err.message);
}
// Async/await
async function fetchData() {
try {
const response = await fetch(url);
const data = await response.json();
return data;
} catch (err) {
console.error('Fetch error:', err);
throw err;
}
}Error-First Callbacks
fs.readFile('file.txt', (err, data) => {
if (err) {
console.error('Error:', err);
return;
}
console.log(data);
});Express Error Handling
// Error middleware (4 parameters)
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(err.status || 500).json({
error: {
message: err.message,
status: err.status
}
});
});
// Async error handling
app.get('/data', async (req, res, next) => {
try {
const data = await getData();
res.json(data);
} catch (err) {
next(err);
}
});Custom Error Classes
class AppError extends Error {
constructor(message, statusCode) {
super(message);
this.statusCode = statusCode;
this.isOperational = true;
Error.captureStackTrace(this, this.constructor);
}
}
class NotFoundError extends AppError {
constructor(message = 'Resource not found') {
super(message, 404);
}
}
class ValidationError extends AppError {
constructor(message = 'Validation failed') {
super(message, 400);
}
}
// Usage
throw new NotFoundError('User not found');Global Error Handlers
// Uncaught exceptions
process.on('uncaughtException', (err) => {
console.error('Uncaught Exception:', err);
process.exit(1);
});
// Unhandled promise rejections
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection:', reason);
process.exit(1);
});Best Practices
- Always handle errors
- Use try-catch for async/await
- Create custom error classes
- Log errors properly
- Don’t expose error details to clients
Interview Tips
- Explain error types: Operational vs programmer errors
- Show try-catch: Synchronous and async
- Demonstrate Express errors: Error middleware
- Discuss custom errors: Error classes
- Mention global handlers: uncaughtException, unhandledRejection
Summary
Handle errors using try-catch, error-first callbacks, and Express error middleware. Create custom error classes for different error types. Use global handlers for uncaught exceptions and unhandled rejections. Always log errors and handle them gracefully.
Test Your Knowledge
Take a quick quiz to test your understanding of this topic.