Skip to content

Commit

Permalink
整理: コアが見つからない例外を追加 (#1307)
Browse files Browse the repository at this point in the history
* refactor: コアが見つからない例外を追加

* fix: 依存関係を明確化

* fix: `CoreNotFound` ハンドラの立ち位置を変更

* fix: エラーメッセージのテストを削除

* fix: `register_` → `configure_` へリネーム
  • Loading branch information
tarepan authored May 28, 2024
1 parent fc73575 commit d939d71
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 8 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions test/e2e/test_missing_core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""コア取得失敗のテスト"""

from fastapi.testclient import TestClient
from syrupy.assertion import SnapshotAssertion


def test_missing_core_422(client: TestClient, snapshot_json: SnapshotAssertion) -> None:
"""存在しないコアを指定するとエラーを返す。"""
response = client.get("/supported_devices", params={"core_version": "4.0.4"})
assert response.status_code == 422
assert snapshot_json == response.json()
9 changes: 6 additions & 3 deletions test/test_core_initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
from unittest.mock import patch

import pytest
from fastapi import HTTPException

from voicevox_engine.core.core_adapter import CoreAdapter
from voicevox_engine.core.core_initializer import CoreManager, get_half_logical_cores
from voicevox_engine.core.core_initializer import (
CoreManager,
CoreNotFound,
get_half_logical_cores,
)
from voicevox_engine.dev.core.mock import MockCoreWrapper


Expand Down Expand Up @@ -93,7 +96,7 @@ def test_cores_get_core_missing() -> None:
core_manager.register_core(core2, "0.0.2")

# Test
with pytest.raises(HTTPException) as _:
with pytest.raises(CoreNotFound):
core_manager.get_core("0.0.3")


Expand Down
2 changes: 2 additions & 0 deletions voicevox_engine/app/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from voicevox_engine import __version__
from voicevox_engine.app.dependencies import deprecated_mutable_api
from voicevox_engine.app.global_exceptions import configure_global_exception_handlers
from voicevox_engine.app.middlewares import configure_middlewares
from voicevox_engine.app.openapi_schema import configure_openapi_schema
from voicevox_engine.app.routers.engine_info import generate_engine_info_router
Expand Down Expand Up @@ -51,6 +52,7 @@ def generate_app(
version=__version__,
)
app = configure_middlewares(app, cors_policy_mode, allow_origin)
app = configure_global_exception_handlers(app)

if disable_mutable_api:
deprecated_mutable_api.enable = False
Expand Down
17 changes: 17 additions & 0 deletions voicevox_engine/app/global_exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""グローバルな例外ハンドラの定義と登録"""

from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse

from voicevox_engine.core.core_initializer import CoreNotFound


def configure_global_exception_handlers(app: FastAPI) -> FastAPI:
"""グローバルな例外ハンドラを app へ設定する。"""

# 指定されたコアが見つからないエラー
@app.exception_handler(CoreNotFound)
async def cnf_exception_handler(request: Request, e: CoreNotFound) -> JSONResponse:
return JSONResponse(status_code=422, content={"message": f"{str(e)}"})

return app
12 changes: 7 additions & 5 deletions voicevox_engine/core/core_initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import sys
from pathlib import Path

from fastapi import HTTPException

from ..utility.core_version_utility import get_latest_version
from ..utility.path_utility import engine_root, get_save_dir
from .core_adapter import CoreAdapter
Expand All @@ -20,6 +18,12 @@ def get_half_logical_cores() -> int:
return logical_cores // 2


class CoreNotFound(Exception):
"""コアが見つからないエラー"""

pass


class CoreManager:
"""コアの集まりを一括管理するマネージャー"""

Expand All @@ -44,9 +48,7 @@ def get_core(self, version: str | None = None) -> CoreAdapter:
return self._cores[self.latest_version()]
elif version in self._cores:
return self._cores[version]

# FIXME: ドメインがずれているのでこのエラーは routers へ持っていく
raise HTTPException(status_code=422, detail="不明なバージョンです")
raise CoreNotFound(f"バージョン {version} のコアが見つかりません")

def has_core(self, version: str) -> bool:
"""指定バージョンのコアが登録されているか否かを返す。"""
Expand Down

0 comments on commit d939d71

Please sign in to comment.