Skip to content

Commit

Permalink
Handle validation errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
nekitdev committed Feb 27, 2024
1 parent f0e5b69 commit 6989219
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
9 changes: 9 additions & 0 deletions melody/kit/core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from argon2 import PasswordHasher
from authlib.integrations.starlette_client import OAuth # type: ignore[import-untyped]
from fastapi.applications import FastAPI
from fastapi.exceptions import RequestValidationError
from fastapi.middleware.cors import CORSMiddleware
from fastapi.requests import Request
from fastapi.responses import JSONResponse
Expand Down Expand Up @@ -95,6 +96,14 @@ async def http_error_handler(request: Request, error: HTTPError) -> JSONResponse

return await error_handler(request, converted_error)

@app.exception_handler(RequestValidationError)
async def validation_error_handler(
request: Request, error: RequestValidationError
) -> JSONResponse:
converted_error = Error.from_validation_error(error)

return await error_handler(request, converted_error)

@app.exception_handler(NormalError)
async def normal_error_handler(request: Request, error: NormalError) -> JSONResponse:
internal_error = InternalError()
Expand Down
14 changes: 14 additions & 0 deletions melody/kit/errors/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from enum import Enum
from typing import ClassVar, Optional, Type

from fastapi import status
from fastapi.exceptions import RequestValidationError
from starlette.exceptions import HTTPException as HTTPError
from typing_aliases import NormalError
from typing_extensions import Self
Expand All @@ -12,6 +14,8 @@

__all__ = ("ErrorCode", "Error", "ErrorData", "ErrorType")

VALIDATION_ERROR = "validation error"


class ErrorCode(Enum):
BASE = 10000
Expand Down Expand Up @@ -180,5 +184,15 @@ def from_http_error(cls, error: HTTPError) -> Self:

return cls(message, code, status_code)

@classmethod
def from_validation_error(cls, error: RequestValidationError) -> Self:
message = VALIDATION_ERROR

status_code = status.HTTP_422_UNPROCESSABLE_ENTITY

code = ErrorCode.from_status_code(status_code)

return cls(message, code, status_code)


ErrorType = Type[Error]
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ include = "melody"
[tool.poetry.scripts]
"melody.bot" = "melody.bot.main:bot"
"melody.kit" = "melody.kit.main:run"
"melody.web" = "melody.web.main:web"
"melody.web" = "melody.web.main:run"

[tool.poetry.dependencies]
# melody
Expand Down Expand Up @@ -146,5 +146,5 @@ wrap_size = 100
display = ["feature", "change", "fix", "security", "deprecation", "removal", "internal"]

[build-system]
requires = ["poetry-core >= 1.8.1"]
requires = ["poetry-core >= 1.9.0"]
build-backend = "poetry.core.masonry.api"

0 comments on commit 6989219

Please sign in to comment.