routes: add a new 404 page.

* Fix a bug where non-GET 404 requests weren't handled.
* Fix a bug where non-HTML 404 requests weren't handled.
* Show a random image from a specified pool on the 404 page.
This commit is contained in:
evazion
2020-12-24 00:07:19 -06:00
parent 039ccfa3af
commit c17678d509
8 changed files with 76 additions and 37 deletions

View File

@@ -124,17 +124,17 @@ class ApplicationController < ActionController::Base
end
end
def render_error_page(status, exception, message: exception.message, template: "static/error", format: request.format.symbol)
def render_error_page(status, exception = nil, message: exception.message, template: "static/error", format: request.format.symbol)
@exception = exception
@expected = status < 500
@message = message.encode("utf-8", invalid: :replace, undef: :replace)
@backtrace = Rails.backtrace_cleaner.clean(@exception.backtrace)
@backtrace = Rails.backtrace_cleaner.clean(@exception.backtrace) if @exception
format = :html unless format.in?(%i[html json xml js atom])
# if InvalidAuthenticityToken was raised, CurrentUser isn't set so we have to use the blank layout.
layout = CurrentUser.user.present? ? "default" : "blank"
DanbooruLogger.log(@exception, expected: @expected)
DanbooruLogger.log(@exception, expected: @expected) if @exception
render template, layout: layout, status: status, formats: format
rescue ActionView::MissingTemplate
render "static/error", layout: layout, status: status, formats: format

View File

@@ -1,4 +1,6 @@
class StaticController < ApplicationController
respond_to :html, :json, :xml
def privacy_policy
end
@@ -6,7 +8,11 @@ class StaticController < ApplicationController
end
def not_found
render plain: "not found", status: :not_found
@pool = Pool.find(Danbooru.config.page_not_found_pool_id) if Danbooru.config.page_not_found_pool_id.present?
@post = @pool.posts.sample if @pool.present?
@artist = @post.tags.select(&:artist?).first if @post.present?
render_error_page(404, nil, template: "static/not_found", message: "Page not found")
end
def error
@@ -38,7 +44,7 @@ class StaticController < ApplicationController
@search = { is_deleted: "false" }
when "posts"
@relation = Post.order(id: :asc)
@serach = {}
@search = {}
when "tags"
@relation = Tag.nonempty
@search = {}

View File

@@ -0,0 +1,26 @@
<% page_title "Page not found" %>
<div id="c-static">
<div id="a-not-found">
<div style="text-align: center">
<h1>Page not found</h1>
<% if @post.present? && @artist.present? %>
<p>
<%= link_to @post do %>
<%= tag.img src: @post.large_file_url %>
<% end %>
<p class="fineprint">
<%= link_to "post ##{@post.id}", @post %>
by <%= link_to @artist.name, posts_path(tags: @artist.name), class: tag_class(@artist) %>
</p>
</p>
<% else %>
<p>Nobody here but us chickens!</p>
<% end %>
<p><%= link_to "Return to previous page", :back %></p>
</div>
</div>
</div>