Skip to content

Commit

Permalink
admin server: display more descriptive 404 message for hidden pages
Browse files Browse the repository at this point in the history
  • Loading branch information
dairiki committed Jul 27, 2022
1 parent 8e2b605 commit d1057c0
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions lektor/admin/modules/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ def _checked_send_file(filename: Filename, mimetype: Optional[str] = None) -> Re
return resp


class HiddenRecordException(NotFound):
"""Exception thrown when a request is made for a hidden page."""

def __init__(self, source: SourceObject) -> None:
super().__init__(description=f"Record is hidden: {source!r}")
self.source = source


class ArtifactServer:
"""Resolve url_path to a Lektor source object, build it, serve the result.
Expand All @@ -104,7 +112,7 @@ def resolve_url_path(self, url_path: str) -> SourceObject:
Raise NotFound if resolution fails.
"""
source = self.lektor_ctx.pad.resolve_url_path(url_path)
source = self.lektor_ctx.pad.resolve_url_path(url_path, include_invisible=True)
if source is None:
abort(404)
return source
Expand Down Expand Up @@ -180,6 +188,9 @@ def serve_artifact(self, url_path: str) -> Response:
):
return append_slash_redirect(request.environ)

if source.is_hidden:
raise HiddenRecordException(source)

if isinstance(source, Directory):
# Special case for asset directories: resolve to index.html
source = self.resolve_directory_index(source)
Expand Down Expand Up @@ -230,6 +241,8 @@ def serve_file(path: str) -> Response:
def serve_artifact_or_file(path: str) -> Response:
try:
return serve_artifact(path)
except HiddenRecordException:
raise
except NotFound:
return serve_file(path)

Expand All @@ -238,5 +251,5 @@ def serve_artifact_or_file(path: str) -> Response:
def serve_error_page(error: NotFound) -> ResponseReturnValue:
try:
return serve_artifact("404.html"), 404
except NotFound as e:
return e
except NotFound:
return error

0 comments on commit d1057c0

Please sign in to comment.