Skip to content

Commit 580188a

Browse files
committed
Custom error responses
1 parent 5a3b02e commit 580188a

File tree

10 files changed

+63
-200
lines changed

10 files changed

+63
-200
lines changed

app/controllers/errors_controller.rb

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class ErrorsController < ApplicationController
2+
def not_found
3+
respond_to do |format|
4+
format.html { render status: :not_found }
5+
format.json { render json: { error: "not found" }, status: :not_found }
6+
format.any { head :not_found }
7+
end
8+
end
9+
10+
def unprocessable
11+
respond_to do |format|
12+
format.html { render status: :unprocessable_entity }
13+
format.json { render json: { error: "unprocessable" }, status: :unprocessable_entity }
14+
format.any { head :unprocessable_entity }
15+
end
16+
end
17+
18+
def internal
19+
respond_to do |format|
20+
format.html { render status: :internal_server_error }
21+
format.json { render json: { error: "internal server error" }, status: :internal_server_error }
22+
format.any { head :internal_server_error }
23+
end
24+
end
25+
end

app/views/errors/internal.html.erb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div class="container-sm text-center">
2+
<h2>Oops, We've had a problem at our end.</h2>
3+
<p>Hopefully this a temporary setback. Try again in a few moments.</p>
4+
</div>

app/views/errors/not_found.html.erb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div class="container-sm text-center">
2+
<h2>We can't find whatever it was you were looking for.</h2>
3+
<p>It may have been deleted, or might not even exist.</p>
4+
</div>
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div class="container-sm text-center">
2+
<h2>Unprocessable request</h2>
3+
<p>Check your request parameters and try again.</p>
4+
</div>

config/application.rb

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module Diff
2323
class Application < Rails::Application
2424
# Initialize configuration defaults for originally generated Rails version.
2525
config.load_defaults 7.0
26+
config.exceptions_app = routes
2627

2728
# Configuration for the application, engines, and railties goes here.
2829
#

config/routes.rb

+4
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,9 @@
2424

2525
get '/diff', to: 'jobs#diff', as: :diff
2626

27+
get '/404', to: 'errors#not_found'
28+
get '/422', to: 'errors#unprocessable'
29+
get '/500', to: 'errors#internal'
30+
2731
root "home#index"
2832
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.
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require 'test_helper'
2+
3+
class ErrorsControllerTest < ActionDispatch::IntegrationTest
4+
test 'renders 404' do
5+
get '/404'
6+
assert_response :not_found
7+
assert_template 'errors/not_found'
8+
end
9+
10+
test 'renders 422' do
11+
get '/422'
12+
assert_response :unprocessable_entity
13+
assert_template 'errors/unprocessable'
14+
end
15+
16+
test 'renders 500' do
17+
get '/500'
18+
assert_response :internal_server_error
19+
assert_template 'errors/internal'
20+
end
21+
end

0 commit comments

Comments
 (0)