refactor: simplify condition (#2775)
This commit is contained in:
parent
09b1623bb0
commit
edfd297bf3
1 changed files with 21 additions and 23 deletions
|
|
@ -26,33 +26,31 @@ class InvoiceError(Exception):
|
||||||
def render_html_error(request: Request, exc: Exception) -> Optional[Response]:
|
def render_html_error(request: Request, exc: Exception) -> Optional[Response]:
|
||||||
# Only the browser sends "text/html" request
|
# Only the browser sends "text/html" request
|
||||||
# not fail proof, but everything else get's a JSON response
|
# not fail proof, but everything else get's a JSON response
|
||||||
|
if not request.headers:
|
||||||
|
return None
|
||||||
|
if "text/html" not in request.headers.get("accept", ""):
|
||||||
|
return None
|
||||||
|
|
||||||
if (
|
if (
|
||||||
request.headers
|
isinstance(exc, HTTPException)
|
||||||
and "accept" in request.headers
|
and exc.headers
|
||||||
and "text/html" in request.headers["accept"]
|
and "token-expired" in exc.headers
|
||||||
):
|
):
|
||||||
if (
|
response = RedirectResponse("/")
|
||||||
isinstance(exc, HTTPException)
|
response.delete_cookie("cookie_access_token")
|
||||||
and exc.headers
|
response.delete_cookie("is_lnbits_user_authorized")
|
||||||
and "token-expired" in exc.headers
|
response.set_cookie("is_access_token_expired", "true")
|
||||||
):
|
return response
|
||||||
response = RedirectResponse("/")
|
|
||||||
response.delete_cookie("cookie_access_token")
|
|
||||||
response.delete_cookie("is_lnbits_user_authorized")
|
|
||||||
response.set_cookie("is_access_token_expired", "true")
|
|
||||||
return response
|
|
||||||
|
|
||||||
status_code: int = (
|
status_code: int = (
|
||||||
exc.status_code
|
exc.status_code
|
||||||
if isinstance(exc, HTTPException)
|
if isinstance(exc, HTTPException)
|
||||||
else HTTPStatus.INTERNAL_SERVER_ERROR
|
else HTTPStatus.INTERNAL_SERVER_ERROR
|
||||||
)
|
)
|
||||||
|
|
||||||
return template_renderer().TemplateResponse(
|
return template_renderer().TemplateResponse(
|
||||||
request, "error.html", {"err": f"Error: {exc!s}"}, status_code
|
request, "error.html", {"err": f"Error: {exc!s}"}, status_code
|
||||||
)
|
)
|
||||||
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def register_exception_handlers(app: FastAPI):
|
def register_exception_handlers(app: FastAPI):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue