Skip to content

Commit

Permalink
FIX: 未処理の例外が発生するとCORSMiddlewareが適用されない問題に対するワークアラウンド (#969)
Browse files Browse the repository at this point in the history
  • Loading branch information
sabonerune authored Jan 3, 2024
1 parent 6452c89 commit 0881648
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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__
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down

0 comments on commit 0881648

Please sign in to comment.