Error Handling
ExpressPro provides a robust error handling system with a custom error class and global error handler.
AppError Class
The AppError
class extends the native Error
class and adds HTTP status code support.
import {AppError} from 'expresspro';
// Create a custom error
throw new AppError('Resource not found', 404);
Properties
message
(string): Error messagestatusCode
(number): HTTP status codestack
(string): Error stack trace
Global Error Handler
ExpressPro includes a global error handler middleware that catches and processes all errors.
import express from 'expresspro';
const app = express();
// Add error handler middleware
app.use(express.error);
Error Response Format
{
success: false,
message: string,
data: {}
}
Usage Examples
Basic Error Handling
import express from 'expresspro';
const app = express();
// Route with error
app.get('/users/:id', express.asyncHandler(async (req, res) => {
const user = await findUser(req.params.id);
if (!user) {
throw new express.AppError('User not found', 404);
}
res.json({ user });
}));
// Add error handler
app.use(express.error);
Custom Error Handler
- In case of you don't want to built in error handler. You can define your custom handler. (Not recommended Use if needed)
import express from 'expresspro';
const app = express();
// Custom error handler
app.use((err, req, res, next) => {
if (err instanceof express.AppError) {
return res.status(err.statusCode).json({
success: false,
message: err.message,
error: {
statusCode: err.statusCode,
message: err.message
}
});
}
// Handle other errors
return res.status(500).json({
success: false,
message: 'Internal server error',
error: {
statusCode: 500,
message: err.message
}
});
});
Best Practices
- Always use
express.asyncHandler
for async route handlers - Throw
AppError
for known error cases - Use appropriate HTTP status codes
- Keep error messages user-friendly
- Log errors for debugging
- Don't expose sensitive information in error messages
Common HTTP Status Codes
- 400: Bad Request
- 401: Unauthorized
- 403: Forbidden
- 404: Not Found
- 409: Conflict
- 422: Unprocessable Entity
- 500: Internal Server Error
Error Handling with Async Handler
import express from 'expresspro';
const app = express();
app.get('/async-route', express.asyncHandler(async (req, res) => {
// No need for try-catch
const data = await someAsyncOperation();
if (!data) {
throw new express.AppError('Data not found', 404);
}
res.json({ data });
}));
Next Steps
- Explore Response Utilities for standardized responses
- Learn about Async Handler for clean async code