StatusFlow provides a hierarchy of exception classes that make error handling in Express applications cleaner and more expressive. Instead of manually managing status codes and messages, you can throw semantic exceptions that are automatically handled by the middleware.
Use when the client sends invalid or malformed data.
import { BadRequestException } from 'status-flow';// Basic usagethrow new BadRequestException();// Default message: "Bad Request"// With custom messagethrow new BadRequestException('Invalid email format');// With detailsthrow new BadRequestException('Validation failed', { errors: [ { field: 'email', message: 'Must be a valid email' }, { field: 'age', message: 'Must be at least 18' } ]});
Use when authentication is required but not provided or invalid.
import { UnauthorizedException } from 'status-flow';// Basic usagethrow new UnauthorizedException();// Default message: "Unauthorized"// With custom messagethrow new UnauthorizedException('Invalid authentication token');// With detailsthrow new UnauthorizedException('Token expired', { expiredAt: '2026-03-07T10:00:00Z', tokenId: 'abc123'});
Use when the user is authenticated but doesn’t have permission for the requested resource.
import { ForbiddenException } from 'status-flow';// Basic usagethrow new ForbiddenException();// Default message: "Forbidden"// With custom messagethrow new ForbiddenException('Insufficient permissions to access this resource');// With detailsthrow new ForbiddenException('Admin access required', { userRole: 'user', requiredRole: 'admin', resource: '/api/admin/users'});
import { NotFoundException } from 'status-flow';// Basic usagethrow new NotFoundException();// Default message: "Not Found"// With custom messagethrow new NotFoundException('User not found');// With detailsthrow new NotFoundException('Product not found', { productId: '12345', searchedIn: 'products_table'});
import { InternalServerErrorException } from 'status-flow';// Basic usagethrow new InternalServerErrorException();// Default message: "Internal Server Error"// With custom messagethrow new InternalServerErrorException('Database connection failed');// With detailsthrow new InternalServerErrorException('Service unavailable', { service: 'payment-processor', error: 'Connection timeout', retryAfter: 60});
When to use:
Database connection failures
External service errors
Unexpected exceptions
Configuration errors
Avoid exposing sensitive error details in production. Use the details field carefully and sanitize information before sending to clients.
HTTP exceptions work seamlessly with Express error-handling middleware. The library provides a middleware that automatically catches these exceptions and formats them into proper HTTP responses.