main
py 100 lines 3.56 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3 from http import HTTPStatus
4
5 from vllm.exceptions import GenerationError, RetryableRequestError
6 from vllm.logger import init_logger
7
8 from ..engine.protocol import ErrorInfo, ErrorResponse
9 from .utils import sanitize_message
10
11 logger = init_logger(__name__)
12
13
14 def create_error_response(
15 message: str | Exception,
16 err_type: str = "BadRequestError",
17 status_code: HTTPStatus = HTTPStatus.BAD_REQUEST,
18 param: str | None = None,
19 ) -> ErrorResponse:
20 exc: Exception | None = None
21
22 if isinstance(message, Exception):
23 exc = message
24 logger.debug(
25 "create_error_response called with %s: %s", type(exc).__name__, exc
26 )
27
28 from vllm.exceptions import (
29 GracefulHTTPError,
30 VLLMClientError,
31 VLLMNotFoundError,
32 VLLMServerError,
33 VLLMUnprocessableEntityError,
34 VLLMValidationError,
35 )
36
37 if isinstance(exc, GracefulHTTPError):
38 err_type = HTTPStatus(exc.http_status).phrase
39 status_code = exc.http_status
40 param = None
41 elif isinstance(exc, VLLMValidationError):
42 err_type = "BadRequestError"
43 status_code = HTTPStatus.BAD_REQUEST
44 param = exc.parameter
45 elif isinstance(exc, VLLMUnprocessableEntityError):
46 err_type = "UnprocessableEntityError"
47 status_code = HTTPStatus.UNPROCESSABLE_ENTITY
48 param = exc.parameter
49 elif isinstance(exc, VLLMNotFoundError):
50 err_type = "NotFoundError"
51 status_code = HTTPStatus.NOT_FOUND
52 param = None
53 elif isinstance(exc, VLLMClientError):
54 # Any other client-caused error defaults to 400.
55 err_type = "BadRequestError"
56 status_code = HTTPStatus.BAD_REQUEST
57 param = None
58 elif isinstance(exc, GenerationError):
59 err_type = (
60 "ServiceUnavailableError"
61 if isinstance(exc, RetryableRequestError)
62 else "InternalServerError"
63 )
64 status_code = exc.status_code
65 param = None
66 elif isinstance(exc, VLLMServerError):
67 # Any other server-caused error defaults to 500.
68 err_type = "InternalServerError"
69 status_code = HTTPStatus.INTERNAL_SERVER_ERROR
70 param = None
71 # Fallback for raw exceptions not yet migrated to VLLMError.
72 # TODO(zqzten): remove these fallback handlers after migration to VLLMError
73 elif isinstance(exc, (ValueError, TypeError, OverflowError)):
74 err_type = "BadRequestError"
75 status_code = HTTPStatus.BAD_REQUEST
76 param = None
77 elif isinstance(exc, NotImplementedError):
78 err_type = "NotImplementedError"
79 status_code = HTTPStatus.NOT_IMPLEMENTED
80 param = None
81 elif any(cls.__name__ == "TemplateError" for cls in type(exc).__mro__):
82 # jinja2.TemplateError and its subclasses (avoid importing jinja2)
83 err_type = "BadRequestError"
84 status_code = HTTPStatus.BAD_REQUEST
85 param = None
86 else:
87 err_type = "InternalServerError"
88 status_code = HTTPStatus.INTERNAL_SERVER_ERROR
89 param = None
90
91 message = str(exc)
92
93 return ErrorResponse(
94 error=ErrorInfo(
95 message=sanitize_message(message),
96 type=err_type,
97 code=status_code.value,
98 param=param,
99 )
100 )