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 22, 2022
1 parent 9b179cb commit 8f17c81
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion 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(Exception):
"""Exception thrown when a request is made for a hidden page."""

def __init__(self, source: SourceObject) -> None:
super().__init__(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 @@ -240,3 +251,8 @@ def serve_error_page(error: NotFound) -> ResponseReturnValue:
return serve_artifact("404.html"), 404
except NotFound as e:
return e


@bp.errorhandler(HiddenRecordException)
def hidden_page(error: HiddenRecordException) -> NotFound:
return NotFound(description=str(error))

0 comments on commit 8f17c81

Please sign in to comment.