Overview
StatusFlow provides seamless integration with Express through middleware and exception classes. This guide walks you through setting up StatusFlow in your Express application for comprehensive error handling.Installation
First, ensure you have StatusFlow and Express installed:Integration Steps
Import StatusFlow components
Import the necessary middleware and exception classes from StatusFlow:
StatusFlow provides two middleware options:
statusFlowMiddleware: For bilingual responses with detailed HTTP status informationhttpErrorMiddleware: For simpler HTTP exception handling
Add error handling middleware
Register the StatusFlow middleware after all your routes:
- statusFlowMiddleware
- httpErrorMiddleware
Use This middleware:
statusFlowMiddleware for bilingual responses with comprehensive HTTP status information:- Detects language from
x-langheader (enores) - Returns detailed HTTP status metadata
- Includes possible causes and descriptions
- Supports custom messages via
overrideMessage
Complete Working Example
Here’s a complete Express application with StatusFlow integration:Testing with Different Languages
When usingstatusFlowMiddleware, you can test language support:
Best Practices
Middleware Order
Always register error handling middleware last, after all routes. This ensures it catches errors from all endpoints.
Throw, Don't Return
Use
throw new Exception() in your routes. Express will automatically catch thrown errors and pass them to your error middleware.Add Context
Include relevant details in the exception’s
details parameter. This helps with debugging and provides useful information to API consumers.Async Error Handling
For async routes, use try-catch blocks or express-async-errors package. See the Error Handling guide for details.
Next Steps
Error Handling Patterns
Learn different error handling patterns and async error handling
Custom Responses
Customize response messages and add extra fields
TypeScript Usage
Type-safe error handling with TypeScript
API Reference
Explore the middleware API documentation