HTML error formatting rebuilt on top of BasePage, and using external module niceback to do the tracebacks.
This commit is contained in:
parent
faf1ff8d4f
commit
77bdfa14ed
@ -23,7 +23,7 @@ from traceback import extract_tb
|
|||||||
from sanic.exceptions import BadRequest, SanicException
|
from sanic.exceptions import BadRequest, SanicException
|
||||||
from sanic.helpers import STATUS_CODES
|
from sanic.helpers import STATUS_CODES
|
||||||
from sanic.response import html, json, text
|
from sanic.response import html, json, text
|
||||||
|
from sanic.pages.base import ErrorPage
|
||||||
|
|
||||||
dumps: t.Callable[..., str]
|
dumps: t.Callable[..., str]
|
||||||
try:
|
try:
|
||||||
@ -159,36 +159,15 @@ class HTMLRenderer(BaseRenderer):
|
|||||||
"{body}"
|
"{body}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def _page(self, full: bool) -> HTTPResponse:
|
||||||
|
page = ErrorPage(super().title, super().text, sys.exc_info()[1], full=full)
|
||||||
|
return html(page.render(), status=self.status, headers=self.headers)
|
||||||
|
|
||||||
def full(self) -> HTTPResponse:
|
def full(self) -> HTTPResponse:
|
||||||
return html(
|
return self._page(full=True)
|
||||||
self.OUTPUT_HTML.format(
|
|
||||||
title=self.title,
|
|
||||||
text=self.text,
|
|
||||||
style=self.TRACEBACK_STYLE,
|
|
||||||
body=self._generate_body(full=True),
|
|
||||||
),
|
|
||||||
status=self.status,
|
|
||||||
)
|
|
||||||
|
|
||||||
def minimal(self) -> HTTPResponse:
|
def minimal(self) -> HTTPResponse:
|
||||||
return html(
|
return self._page(full=False)
|
||||||
self.OUTPUT_HTML.format(
|
|
||||||
title=self.title,
|
|
||||||
text=self.text,
|
|
||||||
style=self.TRACEBACK_STYLE,
|
|
||||||
body=self._generate_body(full=False),
|
|
||||||
),
|
|
||||||
status=self.status,
|
|
||||||
headers=self.headers,
|
|
||||||
)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def text(self):
|
|
||||||
return escape(super().text)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def title(self):
|
|
||||||
return escape(f"⚠️ {super().title}")
|
|
||||||
|
|
||||||
def _generate_body(self, *, full):
|
def _generate_body(self, *, full):
|
||||||
lines = []
|
lines = []
|
||||||
|
@ -50,3 +50,24 @@ class BasePage(ABC):
|
|||||||
@abstractmethod
|
@abstractmethod
|
||||||
def _body(self) -> None:
|
def _body(self) -> None:
|
||||||
...
|
...
|
||||||
|
|
||||||
|
|
||||||
|
class ErrorPage(BasePage):
|
||||||
|
TITLE = "Error while handling your request"
|
||||||
|
|
||||||
|
def __init__(self, title: str, text: str, exc: Exception, full: bool) -> None:
|
||||||
|
super().__init__()
|
||||||
|
self.title = title
|
||||||
|
self.text = text
|
||||||
|
self.exc = exc
|
||||||
|
self.full = full
|
||||||
|
|
||||||
|
def _body(self) -> None:
|
||||||
|
with self.doc.main:
|
||||||
|
self.doc.h1(f"⚠️ {self.title}")
|
||||||
|
if self.full and self.exc:
|
||||||
|
from niceback import html_traceback
|
||||||
|
self.doc(html_traceback(self.exc))
|
||||||
|
else:
|
||||||
|
self.doc.p(self.text)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user