mirror of
https://github.com/PaddlePaddle/FastDeploy.git
synced 2025-10-05 08:37:06 +08:00
[format] Valid para format error info (#4035)
* feat(log):add_request_and_response_log * 报错信息与OpenAI对齐
This commit is contained in:
@@ -28,6 +28,8 @@ import sys
|
||||
import tarfile
|
||||
import time
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
from http import HTTPStatus
|
||||
from importlib.metadata import PackageNotFoundError, distribution
|
||||
from logging.handlers import BaseRotatingHandler
|
||||
from pathlib import Path
|
||||
@@ -38,10 +40,14 @@ import paddle
|
||||
import requests
|
||||
import yaml
|
||||
from aistudio_sdk.snapshot_download import snapshot_download as aistudio_download
|
||||
from fastapi import Request
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.responses import JSONResponse
|
||||
from tqdm import tqdm
|
||||
from typing_extensions import TypeIs, assert_never
|
||||
|
||||
from fastdeploy import envs
|
||||
from fastdeploy.entrypoints.openai.protocol import ErrorInfo, ErrorResponse
|
||||
from fastdeploy.logger.logger import FastDeployLogger
|
||||
|
||||
T = TypeVar("T")
|
||||
@@ -59,6 +65,61 @@ class EngineError(Exception):
|
||||
self.error_code = error_code
|
||||
|
||||
|
||||
class ParameterError(Exception):
|
||||
def __init__(self, param: str, message: str):
|
||||
self.param = param
|
||||
self.message = message
|
||||
super().__init__(message)
|
||||
|
||||
|
||||
class ExceptionHandler:
|
||||
|
||||
# 全局异常兜底处理
|
||||
@staticmethod
|
||||
async def handle_exception(request: Request, exc: Exception) -> JSONResponse:
|
||||
error = ErrorResponse(error=ErrorInfo(message=str(exc), type=ErrorType.INTERNAL_ERROR))
|
||||
return JSONResponse(content=error.model_dump(), status_code=HTTPStatus.INTERNAL_SERVER_ERROR)
|
||||
|
||||
# 处理请求参数验证异常
|
||||
@staticmethod
|
||||
async def handle_request_validation_exception(_: Request, exc: RequestValidationError) -> JSONResponse:
|
||||
errors = exc.errors()
|
||||
if not errors:
|
||||
message = str(exc)
|
||||
param = None
|
||||
else:
|
||||
first_error = errors[0]
|
||||
loc = first_error.get("loc", [])
|
||||
param = loc[-1] if loc else None
|
||||
message = first_error.get("msg", str(exc))
|
||||
err = ErrorResponse(
|
||||
error=ErrorInfo(
|
||||
message=message,
|
||||
type=ErrorType.INVALID_REQUEST_ERROR,
|
||||
code=ErrorCode.MISSING_REQUIRED_PARAMETER if param == "messages" else ErrorCode.INVALID_VALUE,
|
||||
param=param,
|
||||
)
|
||||
)
|
||||
return JSONResponse(content=err.model_dump(), status_code=HTTPStatus.BAD_REQUEST)
|
||||
|
||||
|
||||
class ErrorType(str, Enum):
|
||||
INVALID_REQUEST_ERROR = "invalid_request_error"
|
||||
TIMEOUT_ERROR = "timeout_error"
|
||||
SERVER_ERROR = "server_error"
|
||||
INTERNAL_ERROR = "internal_error"
|
||||
API_CONNECTION_ERROR = "api_connection_error"
|
||||
|
||||
|
||||
class ErrorCode(str, Enum):
|
||||
INVALID_VALUE = "invalid_value"
|
||||
CONTEXT_LENGTH_EXCEEDED = "context_length_exceeded"
|
||||
MODEL_NOT_SUPPORT = "model_not_support"
|
||||
TIMEOUT = "timeout"
|
||||
CONNECTION_ERROR = "connection_error"
|
||||
MISSING_REQUIRED_PARAMETER = "missing_required_parameter"
|
||||
|
||||
|
||||
class ColoredFormatter(logging.Formatter):
|
||||
"""自定义日志格式器,用于控制台输出带颜色"""
|
||||
|
||||
|
Reference in New Issue
Block a user