Skip to content

Commit 9dafff1

Browse files
committed
Add error routes
1 parent 566ff10 commit 9dafff1

File tree

10 files changed

+44
-652
lines changed

10 files changed

+44
-652
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class ErrorsController < ApplicationController
2+
VALID_STATUS_CODES = %w[400 404 406 422 500].freeze
3+
4+
def self.constraints
5+
{ code: Regexp.new(VALID_STATUS_CODES.join("|")) }
6+
end
7+
8+
def show
9+
status_code = VALID_STATUS_CODES.include?(params[:code]) ? params[:code] : 500
10+
respond_to do |format|
11+
format.html { render status: status_code }
12+
format.any { head status_code }
13+
end
14+
end
15+
end

app/views/errors/show.html.erb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<div id="content" class="scm fof">
2+
<h1>Oops,</h1>
3+
<% case response.code.to_i %>
4+
<% when 404 %>
5+
<h2 class="nomargin">The page could not be found.</h2>
6+
<p>
7+
Sorry, but we couldn't find the page <strong><%= h URI(request.original_url).path %></strong>.
8+
Are you sure that page?
9+
</p>
10+
<% else %>
11+
<h2 class="nomargin">An unexpected error occurred.</h2>
12+
<p>
13+
Sorry, but an unexpected error occurred while processing your request.
14+
Please try again later or contact support using the Help link above.
15+
</p>
16+
<% end %>
17+
</h2>
18+
<%= link_to "Go back home", root_path %>
19+
</div>

app/views/layouts/_meta.html.erb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
<% else %>
77
<meta name="robots" content="index, follow">
88
<% end %>
9-
<link rel="canonical" href="<%= request.original_url %>">
10-
<meta property="og:title" content="<%= h page_title %>">
11-
<meta property="og:description" content="<%= h page_description %>">
12-
<meta property="og:image" content="<%= URI(request.original_url).tap { it.path = "/favicon.ico" }.to_s %>">
13-
<meta property="og:url" content="<%= request.original_url %>">
14-
<meta property="og:type" content="website">
9+
<% if controller_name != "errors" %>
10+
<link rel="canonical" href="<%= request.original_url %>">
11+
<meta property="og:title" content="<%= h page_title %>">
12+
<meta property="og:description" content="<%= h page_description %>">
13+
<meta property="og:image" content="<%= URI(request.original_url).tap { it.path = "/favicon.ico" }.to_s %>">
14+
<meta property="og:url" content="<%= request.original_url %>">
15+
<meta property="og:type" content="website">
16+
<% end %>

config/application.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@ class Application < Rails::Application
3838
# config.time_zone = "Central Time (US & Canada)"
3939
# config.eager_load_paths << Rails.root.join("extras")
4040
config.session_store :disabled
41+
config.exceptions_app = routes
4142
end
4243
end

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# root "posts#index"
1414

1515
root "github#index"
16+
match "/:code", to: "errors#show", via: :all, constraints: ErrorsController.constraints
1617

1718
get "/about", to: "about#index", as: :about
1819
get "/help", to: "help#index", as: :help

public/400.html

Lines changed: 0 additions & 134 deletions
This file was deleted.

public/404.html

Lines changed: 0 additions & 128 deletions
This file was deleted.

0 commit comments

Comments
 (0)