Skip to main content
Glama
error_middleware.py1.65 kB
"""Error handling middleware. This module provides global error handling middleware. """ import traceback from collections.abc import Callable from fastapi import Request, Response from fastapi.responses import JSONResponse from starlette.middleware.base import BaseHTTPMiddleware from src.core.config import get_settings from src.core.exceptions import BaseDocxException class ErrorMiddleware(BaseHTTPMiddleware): """Middleware for global error handling. This middleware catches unhandled exceptions and returns appropriate JSON error responses. """ async def dispatch( self, request: Request, call_next: Callable, ) -> Response: """Process the request and handle errors. Args: request: Incoming request. call_next: Next middleware/handler. Returns: Response from the next handler or error response. """ try: return await call_next(request) except BaseDocxException as e: return JSONResponse( status_code=e.status_code, content=e.to_dict(), ) except Exception as e: settings = get_settings() error_detail = { "code": "INTERNAL_ERROR", "message": "An unexpected error occurred", } if settings.debug: error_detail["message"] = str(e) error_detail["traceback"] = traceback.format_exc() return JSONResponse( status_code=500, content={"error": error_detail}, )

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Fu-Jie/MCP-OPENAPI-DOCX'

If you have feedback or need assistance with the MCP directory API, please join our Discord server