Skip to content

Commit

Permalink
Override default validation error.
Browse files Browse the repository at this point in the history
  • Loading branch information
nekitdev committed Feb 27, 2024
1 parent 6989219 commit 42d3f14
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
21 changes: 18 additions & 3 deletions melody/kit/core.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from typing import Any, Dict

from argon2 import PasswordHasher
from authlib.integrations.starlette_client import OAuth # type: ignore[import-untyped]
from fastapi import status
from fastapi.applications import FastAPI
from fastapi.exceptions import RequestValidationError
from fastapi.middleware.cors import CORSMiddleware
Expand All @@ -8,20 +11,23 @@
from redis.asyncio import Redis
from starlette.exceptions import HTTPException as HTTPError
from starlette.middleware.sessions import SessionMiddleware # XXX: use `fastapi` when implemented
from typing_aliases import NormalError
from typing_aliases import NormalError, StringDict

from melody.kit.config import CONFIG
from melody.kit.constants import V1, VERSION_1
from melody.kit.database import Database
from melody.kit.errors.core import Error
from melody.kit.errors.core import Error, ErrorData
from melody.kit.errors.internal import InternalError
from melody.shared.constants import DEFAULT_ENCODING, DEFAULT_ERRORS, STAR
from melody.shared.typing import IntString

__all__ = ("config", "database", "redis", "hasher", "oauth", "app", "v1")

database = Database()
"""The database instance to use."""

config = CONFIG
"""The config instance to use."""

redis = Redis(
host=config.redis.host,
Expand All @@ -30,14 +36,17 @@
encoding_errors=DEFAULT_ERRORS,
decode_responses=True,
)
"""The redis instance to use."""

hasher = PasswordHasher(
time_cost=config.hash.time_cost,
memory_cost=config.hash.memory_cost,
parallelism=config.hash.parallelism,
)
"""The hasher instance to use."""

oauth = OAuth()
"""The oauth instance to use."""

DISCORD = "discord"
DISCORD_AUTHORIZE_URL = "https://discord.com/oauth2/authorize"
Expand Down Expand Up @@ -111,7 +120,13 @@ async def normal_error_handler(request: Request, error: NormalError) -> JSONResp
return await error_handler(request, internal_error)


v1 = FastAPI(title=config.name, version=VERSION_1)
VALIDATION_ERROR = "Validation error"

OVERRIDE_VALIDATION_ERROR: Dict[IntString, StringDict[Any]] = {
status.HTTP_422_UNPROCESSABLE_ENTITY: dict(description=VALIDATION_ERROR, model=ErrorData),
}

v1 = FastAPI(title=config.name, version=VERSION_1, responses=OVERRIDE_VALIDATION_ERROR)
"""The `/api/v1` application."""

app.mount(V1, v1)
Expand Down
1 change: 1 addition & 0 deletions melody/shared/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
)

URLString = Union[URL, str]
IntString = Union[int, str]
AnyString = Union[str, bytes]

0 comments on commit 42d3f14

Please sign in to comment.