No auto registration of fallback error handlers

This commit is contained in:
Adam Hopkins 2023-01-31 10:40:47 +02:00
parent 713abe3cf2
commit 1b43aa5f2f
No known key found for this signature in database
GPG Key ID: 9F85EE6C807303FB
4 changed files with 34 additions and 35 deletions

View File

@ -57,6 +57,7 @@ from sanic.config import SANIC_PREFIX, Config
from sanic.exceptions import ( from sanic.exceptions import (
BadRequest, BadRequest,
SanicException, SanicException,
SanicIsADirectoryError,
ServerError, ServerError,
URLBuildError, URLBuildError,
) )
@ -1578,6 +1579,10 @@ class Sanic(BaseSanic, StartupMixin, metaclass=TouchUpMeta):
TouchUp.run(self) TouchUp.run(self)
self.state.is_started = True self.state.is_started = True
self.exception(SanicIsADirectoryError)(
DirectoryHandler.default_handler
)
self.directory_handler.debug = self.debug self.directory_handler.debug = self.debug
def ack(self): def ack(self):

View File

@ -3,8 +3,6 @@ from __future__ import annotations
from typing import Dict, List, Optional, Tuple, Type from typing import Dict, List, Optional, Tuple, Type
from sanic.errorpages import BaseRenderer, TextRenderer, exception_response from sanic.errorpages import BaseRenderer, TextRenderer, exception_response
from sanic.exceptions import SanicIsADirectoryError
from sanic.handlers.directory import DirectoryHandler
from sanic.log import deprecation, error_logger from sanic.log import deprecation, error_logger
from sanic.models.handler_types import RouteHandler from sanic.models.handler_types import RouteHandler
from sanic.response import text from sanic.response import text
@ -22,19 +20,13 @@ class ErrorHandler:
realtime alerting system. realtime alerting system.
""" """
DEFAULT_HANDLERS = {
(SanicIsADirectoryError, None): DirectoryHandler.default_handler
}
def __init__( def __init__(
self, self,
base: Type[BaseRenderer] = TextRenderer, base: Type[BaseRenderer] = TextRenderer,
): ):
self.cached_handlers: Dict[ self.cached_handlers: Dict[
Tuple[Type[BaseException], Optional[str]], Optional[RouteHandler] Tuple[Type[BaseException], Optional[str]], Optional[RouteHandler]
] = { ] = {}
**self.DEFAULT_HANDLERS # type: ignore
}
self.debug = False self.debug = False
self.base = base self.base = base

View File

@ -15,29 +15,31 @@ class FileInfo(TypedDict):
class AutoIndex(BasePage): # no cov class AutoIndex(BasePage): # no cov
EXTRA_STYLE = dedent( EXTRA_STYLE = dedent(
""" f"""
#breadcrumbs a { text-decoration: none; } #breadcrumbs a:hover {{ text-decoration: underline; }}
#breadcrumbs .path-0 a::before { content: "🏠"; } #breadcrumbs .path-0 a {{ text-decoration: none; }}
#breadcrumbs span:has(> a:hover, > a:focus) * { #breadcrumbs span::after {{
color: #ff0d68; text-shadow: 0 0 1rem; content: "/"; text-decoration: none; padding: 0 0.25em;
} }}
main a { color: inherit; font-weight: bold; } #breadcrumbs .path-0 a::before {{ content: "🏠"; }}
table.autoindex { width: 100%; } #breadcrumbs > span > a {{ color: {BasePage.ACCENT}; }}
table.autoindex tr { display: flex; } main a {{ color: inherit; font-weight: bold; }}
table.autoindex tr:hover { background-color: #eee; } table.autoindex {{ width: 100%; }}
table.autoindex td { margin: 0 0.5rem; } table.autoindex tr {{ display: flex; }}
table.autoindex td:first-child { flex: 1; } table.autoindex tr:hover {{ background-color: #ddd; }}
table.autoindex td:nth-child(2) { text-align: right; } table.autoindex td {{ margin: 0 0.5rem; }}
table.autoindex td:last-child { text-align: right; } table.autoindex td:first-child {{ flex: 1; }}
@media (min-width: 915px) { table.autoindex td:nth-child(2) {{ text-align: right; }}
table.autoindex { font-size: 1.75vw; } table.autoindex td:last-child {{ text-align: right; }}
} @media (min-width: 915px) {{
@media (min-width: 1600px) { table.autoindex {{ font-size: 1.75vw; }}
table.autoindex { font-size: 1.75rem; } }}
} @media (min-width: 1600px) {{
@media (prefers-color-scheme: dark) { table.autoindex {{ font-size: 1.75rem; }}
table.autoindex tr:hover { background-color: #222; } }}
} @media (prefers-color-scheme: dark) {{
table.autoindex tr:hover {{ background-color: #222; }}
}}
""" """
) )
TITLE = "File Browser" TITLE = "File Browser"
@ -67,7 +69,7 @@ class AutoIndex(BasePage): # no cov
self.doc.span(class_=f"path-{i}").__enter__() self.doc.span(class_=f"path-{i}").__enter__()
for i, part in enumerate(p): for i, part in enumerate(p):
path = "/".join(p[: i + 1]) + "/" path = "/".join(p[: i + 1]) + "/"
self.doc.a(f"{part}/", href=path) self.doc.a(part, href=path)
self.doc.__exit__(None, None, None) self.doc.__exit__(None, None, None)
def _file_table(self, files: Iterable[FileInfo]): def _file_table(self, files: Iterable[FileInfo]):

View File

@ -8,9 +8,10 @@ from sanic.application.logo import SVG_LOGO
class BasePage(ABC): # no cov class BasePage(ABC): # no cov
ACCENT = "#ff0d68"
BASE_STYLE = dedent( BASE_STYLE = dedent(
""" """
html { font: 16px monospace; } html { font: 16px monospace; background: #eee; color: #111; }
body { margin: 0; font-size: 1.25rem; } body { margin: 0; font-size: 1.25rem; }
body > * { padding: 1rem 2vw; } body > * { padding: 1rem 2vw; }
@media (max-width: 1200px) { @media (max-width: 1200px) {
@ -30,7 +31,6 @@ class BasePage(ABC): # no cov
a:hover, a:focus { text-decoration: underline; outline: none; } a:hover, a:focus { text-decoration: underline; outline: none; }
#logo { height: 2.75rem; padding: 0.25rem 0; } #logo { height: 2.75rem; padding: 0.25rem 0; }
span.icon { margin-right: 1rem; } span.icon { margin-right: 1rem; }
html { background: #eee; color: #000; }
@media (prefers-color-scheme: dark) { @media (prefers-color-scheme: dark) {
html { background: #111; color: #ccc; } html { background: #111; color: #ccc; }
} }