Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pass kwargs from fastapi error factory to inner error generating fns #111

Merged
merged 1 commit into from
Jul 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions bento_lib/responses/fastapi_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,36 @@ def _log_if_500(logger: logging.Logger, code: int, exc: Exception) -> None:
def http_exception_handler_factory(
logger: logging.Logger,
authz: FastApiAuthMiddleware | None = None,
**kwargs,
) -> Callable[[Request, HTTPException], Response]:
def http_exception_handler(request: Request, exc: HTTPException) -> JSONResponse:
if authz:
authz.mark_authz_done(request)
code = exc.status_code
_log_if_500(logger, code, exc)
return JSONResponse(http_error(code, exc.detail), status_code=code)
return JSONResponse(http_error(code, exc.detail, **kwargs), status_code=code)

return http_exception_handler


def bento_auth_exception_handler_factory(
logger: logging.Logger,
authz: FastApiAuthMiddleware | None = None,
**kwargs,
) -> Callable[[Request, BentoAuthException], Response]:
def bento_auth_exception_handler(request: Request, exc: BentoAuthException) -> JSONResponse:
if authz:
authz.mark_authz_done(request)
code = exc.status_code
_log_if_500(logger, code, exc)
return JSONResponse(http_error(code, exc.message), status_code=code)
return JSONResponse(http_error(code, exc.message, **kwargs), status_code=code)

return bento_auth_exception_handler


def validation_exception_handler_factory(
authz: FastApiAuthMiddleware | None = None,
**kwargs,
) -> Callable[[Request, RequestValidationError], Response]:
def validation_exception_handler(request: Request, exc: RequestValidationError) -> JSONResponse:
if authz:
Expand All @@ -63,6 +66,7 @@ def validation_exception_handler(request: Request, exc: RequestValidationError)
http_error(
code,
*((".".join(map(str, e["loc"])) + ": " + e["msg"])
if e.get("loc") else e["msg"] for e in exc.errors())),
if e.get("loc") else e["msg"] for e in exc.errors()),
**kwargs),
status_code=code)
return validation_exception_handler
Loading