From 08816486a4918fe574da65f9d11beb863f569de5 Mon Sep 17 00:00:00 2001 From: sabonerune <102559104+sabonerune@users.noreply.github.com> Date: Wed, 3 Jan 2024 18:01:04 +0900 Subject: [PATCH] =?UTF-8?q?FIX:=20=E6=9C=AA=E5=87=A6=E7=90=86=E3=81=AE?= =?UTF-8?q?=E4=BE=8B=E5=A4=96=E3=81=8C=E7=99=BA=E7=94=9F=E3=81=99=E3=82=8B?= =?UTF-8?q?=E3=81=A8CORSMiddleware=E3=81=8C=E9=81=A9=E7=94=A8=E3=81=95?= =?UTF-8?q?=E3=82=8C=E3=81=AA=E3=81=84=E5=95=8F=E9=A1=8C=E3=81=AB=E5=AF=BE?= =?UTF-8?q?=E3=81=99=E3=82=8B=E3=83=AF=E3=83=BC=E3=82=AF=E3=82=A2=E3=83=A9?= =?UTF-8?q?=E3=82=A6=E3=83=B3=E3=83=89=20(#969)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- run.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/run.py b/run.py index 33a2b15ca..ba29fc05e 100644 --- a/run.py +++ b/run.py @@ -14,7 +14,7 @@ from io import BytesIO, TextIOWrapper from pathlib import Path from tempfile import NamedTemporaryFile, TemporaryFile -from typing import Annotated, Any, Dict, List, Optional +from typing import Annotated, Any, Optional import soundfile import uvicorn @@ -25,6 +25,7 @@ from fastapi.templating import Jinja2Templates from pydantic import ValidationError from starlette.background import BackgroundTask +from starlette.middleware.errors import ServerErrorMiddleware from starlette.responses import FileResponse from voicevox_engine import __version__ @@ -145,15 +146,15 @@ def set_output_log_utf8() -> None: def generate_app( - tts_engines: Dict[str, TTSEngine], - cores: Dict[str, CoreAdapter], + tts_engines: dict[str, TTSEngine], + cores: dict[str, CoreAdapter], latest_core_version: str, setting_loader: SettingLoader, preset_manager: PresetManager, cancellable_engine: CancellableEngine | None = None, root_dir: Optional[Path] = None, cors_policy_mode: CorsPolicyMode = CorsPolicyMode.localapps, - allow_origin: Optional[List[str]] = None, + allow_origin: Optional[list[str]] = None, disable_mutable_api: bool = False, ) -> FastAPI: if root_dir is None: @@ -165,6 +166,16 @@ def generate_app( version=__version__, ) + # 未処理の例外が発生するとCORSMiddlewareが適用されない問題に対するワークアラウンド + # ref: https://github.com/VOICEVOX/voicevox_engine/issues/91 + async def global_execution_handler(request: Request, exc: Exception) -> Response: + return JSONResponse( + status_code=500, + content="Internal Server Error", + ) + + app.add_middleware(ServerErrorMiddleware, handler=global_execution_handler) + # CORS用のヘッダを生成するミドルウェア localhost_regex = "^https?://(localhost|127\\.0\\.0\\.1)(:[0-9]+)?$" compiled_localhost_regex = re.compile(localhost_regex)