Skip to content

Commit b5a02e8

Browse files
colinxfleminglomky
andauthored
add starter of custom 404/422/500 routing (#2560)
* add starter of custom 404/422/500 routing * proper layout routing * use btn, stash for now * don't use a separate layout for errors, add conditionals to app instead * don't really need this to render pages, it kicks it to errors controller regardless * don't need these anymore * re-add csrf - needed for sign out, it turns out * Update app/views/errors/error_500.html.erb Co-authored-by: Kat Tipton <[email protected]> Co-authored-by: Kat Tipton <[email protected]>
1 parent b47119c commit b5a02e8

12 files changed

+54
-206
lines changed

app/controllers/application_controller.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ class ApplicationController < ActionController::Base
66
# For APIs, you may want to use :null_session instead.
77
protect_from_forgery prepend: true, with: :exception
88

9-
prepend_before_action :authenticate_user!
10-
prepend_before_action :confirm_user_not_disabled!, unless: :devise_controller?
9+
prepend_before_action :authenticate_user!, unless: -> { try(:errors_controller?) }
10+
prepend_before_action :confirm_user_not_disabled!, unless: -> { devise_controller? || try(:errors_controller?) }
1111

1212
if Rails.env.development?
1313
before_action :confirm_tenant_set_development

app/controllers/errors_controller.rb

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class ErrorsController < ApplicationController
2+
def errors_controller?
3+
true
4+
end
5+
6+
def error_404; end
7+
8+
def error_422; end
9+
10+
def error_500; end
11+
end

app/views/errors/error_404.html.erb

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<% content_for :title do %>DARIA - 404<% end %>
2+
<% content_for :error_page do %>error<% end %>
3+
4+
<h2>Sorry! The page you're looking for doesn't exist. (404)</h2>
5+
6+
<p>This page may have moved, or been removed entirely.</p>

app/views/errors/error_422.html.erb

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<% content_for :title do %>DARIA - 422<% end %>
2+
<% content_for :error_page do %>error<% end %>
3+
4+
<h2>Sorry! Something went wrong. (422)</h2>
5+
6+
<p>Something about the data looked weird - try adjusting it and give it another shot.</p>

app/views/errors/error_500.html.erb

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<% content_for :title do %>DARIA - 500<% end %>
2+
<% content_for :error_page do %>error<% end %>
3+
4+
<h2>Sorry! Something went wrong. (500)</h2>
5+
6+
<p>This is probably an us problem - try again later, and have your organization let the Daria maintainers know if it persists.</p>

app/views/layouts/application.html.erb

+15-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
55
<title><%= content_for?(:title) ? yield(:title) : "DARIA" %></title>
6-
<meta name="description" content="<%= content_for?(:description) ? yield(:description) : "DCAF" %>">
6+
<meta name="description" content="<%= content_for?(:description) ? yield(:description) : "DARIA" %>">
77
<meta name='ROBOTS' content='NOINDEX, NOFOLLOW'>
88
<meta name="google-site-verification" content="<%= ENV['GOOGLE_SITE_VERIFICATION'] %>" />
99

@@ -14,21 +14,33 @@
1414
<%= stylesheet_link_tag 'application', media: 'all' %>
1515

1616
<%= javascript_include_tag "application", "data-turbo-track": "reload", defer: true %>
17-
<%= content_for :render_async %>
1817

18+
<% if !content_for? :error_page %>
19+
<%= content_for :render_async %>
20+
<% end %>
1921
</head>
22+
2023
<body>
2124
<% if current_user.present? %>
2225
<header>
2326
<%= render 'layouts/navigation' %>
2427
</header>
2528
<% end %>
29+
2630
<main class="container" role="main">
27-
<%= render 'layouts/messages' %>
31+
<% if !content_for? :error_page %>
32+
<%= render 'layouts/messages' %>
33+
<% end %>
2834

2935
<br>
3036
<%= yield %>
3137

38+
<% if content_for? :error_page %>
39+
<p>
40+
<%= link_to 'Back to homepage', root_path, class: 'btn btn-primary btn-lg text-white' %>
41+
</p>
42+
<% end %>
43+
3244
<% if current_user.present? %>
3345
<footer>
3446
<%= render 'layouts/footer' %>

config/application.rb

+3
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,8 @@ class Application < Rails::Application
5757
#
5858
# config.time_zone = "Central Time (US & Canada)"
5959
# config.eager_load_paths << Rails.root.join("extras")
60+
61+
# Custom exceptions pages
62+
config.exceptions_app = self.routes
6063
end
6164
end

config/environments/development.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
config.eager_load = false
1313

1414
# Show full error reports.
15-
config.consider_all_requests_local = true
15+
config.consider_all_requests_local = false
1616

1717
# Enable server timing
1818
config.server_timing = true

config/routes.rb

+4
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,8 @@
6464
get '/users/edit' => 'devise/registrations#edit', as: 'edit_user_registration'
6565
put '/users' => 'devise/registrations#update', as: 'registration'
6666
end
67+
68+
match '/404' => 'errors#error_404', via: [:get, :post, :put, :patch]
69+
match '/422' => 'errors#error_422', via: [:get, :post, :put, :patch]
70+
match '/500' => 'errors#error_500', via: [:get, :post, :put, :patch]
6771
end

public/404.html

-67
This file was deleted.

public/422.html

-67
This file was deleted.

public/500.html

-66
This file was deleted.

0 commit comments

Comments
 (0)