-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.py
37 lines (25 loc) · 1.1 KB
/
errors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"Exceptions and their handlers."
from http import HTTPStatus as HTTP
import urllib
from fasthtml.common import Response, RedirectResponse
__all__ = ["Error", "error_handler", "NotAllowed", "not_allowed_handler", "HTTP"]
class Error(Exception):
"Custom exception; return response with message and status code."
def __init__(self, message, status_code=HTTP.BAD_REQUEST):
super().__init__(message)
self.status_code = status_code
def error_handler(request, exc):
"Return a response with the message and status code."
return Response(content=str(exc), status_code=exc.status_code)
class NotAllowed(Exception):
"Not allowed to access a page."
pass
def not_allowed_handler(request, exc):
"""If logged in, then forbidden since authorization failed.
If not logged in, then redirect to login.
"""
if request.scope.get("current_user"):
return Response(content="Forbidden", status_code=HTTP.FORBIDDEN)
else:
path = urllib.parse.urlencode({"path": request.url.path})
return RedirectResponse(f"/user/login?{path}", status_code=HTTP.SEE_OTHER)