feat: enable custom layouts for error pages (Fixes #755)#771
feat: enable custom layouts for error pages (Fixes #755)#771AmanKashyap0807 wants to merge 1 commit into
Conversation
|
I tried out something similar to this and it worked. However, I don't know if we should fix the error view files to app/resources/errors/views, especially in regard to Stipple. We should leave it up to the maintainers to decide the best way to handle this. Thanks for your efforts to solve this issue! julia> julia> Genie.Generator.newapp_mvc("TestApp");
julia> mkpath("app/resources/errors/views")
"app/resources/errors/views"
julia> open("app/resources/errors/views/error_404.jl.html", "w") do f
write(f, "<h1>Custom 404</h1><p>\$(vars(:message))</p>")
end
43
julia> open("app/resources/errors/views/error_500.jl.html", "w") do f
write(f, "<h1>Custom 500</h1><p>\$(vars(:message))</p><span>\$(vars(:info))</span>")
end
70
julia> Genie.config.error_layout = :app
:app
julia> res404 = Genie.Router.error("Page Not Found", MIME"text/html", Val(404))
HTTP.Messages.Response:
"""
HTTP/1.1 404 Not Found
Content-Type: text/html; charset=utf-8
<!DOCTYPE html><html lang="en">
<head>
<meta charset="utf-8" />
<title>Genie :: The Highly Productive Julia Web Framework</title>
</head>
<body>
<h1>Custom 404</h1>
<p>Page Not Found</p>
</body></html>"""
julia> res505 = Genie.Router.error("Crash!", MIME"text/html", Val(500), error_info="Stacktrace info")
HTTP.Messages.Response:
"""
HTTP/1.1 500 Internal Server Error
Content-Type: text/html; charset=utf-8
<!DOCTYPE html><html lang="en">
<head>
<meta charset="utf-8" />
<title>Genie :: The Highly Productive Julia Web Framework</title>
</head>
<body>
<h1>Custom 500</h1>
<p>Crash!</p>
<span>Stacktrace info</span>
</body></html>""" |
|
Thanks for the feedback and for testing it out! @andreeco You raise a valid point about the directory structure ( I decided to treat "Errors" like a standard Genie Resource (similar to how we have However, since this is a new feature, I am totally open to moving these files to a different location (like |
Fixes #755
Description
This PR introduces a standardized way to render HTML error pages (404, 500, etc.) using the application's main layout. This resolves the issue where error pages were "naked" (missing headers/footers), making it difficult to include mandatory legal links (Imprint/Privacy) on error screens.
Changes
Genie.config.error_layout(defaults tonothingfor backward compatibility).serve_error_pageto check this configuration.error_layoutis set, it renders the specific error view (e.g.,:errors, :error_404) wrapped in the defined layout.nothing, it falls back to the existing behavior (serving static error files).README.mdto mention the new configuration option.How to Test (Integration)
app/layouts/legal.jl.htmlcontaining a footer.resources/errors/views/error_404.jl.html.Genie.config.error_layout = :legalinconfig/initializers.jl.Verification of Implementation (Unit Simulation)
I verified this locally using a standalone script that mocks the view/layout registration and asserts that the Router produces HTML containing both the layout footer and the error message.
Click to see the verification script used