| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | from http import HTTPStatus |
| 4 | |
| 5 | from fastapi import HTTPException, Request |
| 6 | from starlette.responses import JSONResponse |
| 7 | |
| 8 | from vllm.entrypoints.serve.engine.protocol import ErrorInfo, ErrorResponse |
| 9 | from vllm.logger import init_logger |
| 10 | |
| 11 | from ..utils import sanitize_message |
| 12 | |
| 13 | logger = init_logger(__name__) |
| 14 | |
| 15 | |
| 16 | async def http_exception_handler(req: Request, exc: HTTPException): |
| 17 | if req.app.state.args.log_error_stack: |
| 18 | logger.exception( |
| 19 | "HTTPException caught. Request id: %s", |
| 20 | req.state.request_metadata.request_id |
| 21 | if hasattr(req.state, "request_metadata") |
| 22 | else None, |
| 23 | ) |
| 24 | err = ErrorResponse( |
| 25 | error=ErrorInfo( |
| 26 | message=sanitize_message(exc.detail), |
| 27 | type=HTTPStatus(exc.status_code).phrase, |
| 28 | code=exc.status_code, |
| 29 | ) |
| 30 | ) |
| 31 | return JSONResponse(err.model_dump(), status_code=exc.status_code) |