From b133e2c610686d349385e4a6fea3b63409ddb338 Mon Sep 17 00:00:00 2001 From: Yuki Nishijima Date: Sun, 4 Aug 2024 11:46:59 +0900 Subject: [PATCH] Keep the compatibility with Rack --- README.md | 2 +- .../rambulance/templates/exceptions_app.rb | 2 +- lib/generators/rambulance/templates/rambulance.rb | 2 +- ...ntity.html.erb => unprocessable_content.html.erb} | 2 +- ....jbuilder => unprocessable_content.json.jbuilder} | 0 lib/rambulance/exceptions_app.rb | 12 +++++++++++- test/exceptions_app_test.rb | 6 ++++++ 7 files changed, 21 insertions(+), 5 deletions(-) rename lib/generators/rambulance/templates/views/{unprocessable_entity.html.erb => unprocessable_content.html.erb} (94%) rename lib/generators/rambulance/templates/views/{unprocessable_entity.json.jbuilder => unprocessable_content.json.jbuilder} (100%) diff --git a/README.md b/README.md index 3be3d0d..50985f7 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ Then do the following: ```ruby # config/initializers/rambulance.rb config.rescue_responses = { - "ActiveRecord::RecordNotUnique" => :unprocessable_entity, + "ActiveRecord::RecordNotUnique" => :unprocessable_content, "CanCan::AccessDenied" => :forbidden, "YourCustomException" => :not_found } diff --git a/lib/generators/rambulance/templates/exceptions_app.rb b/lib/generators/rambulance/templates/exceptions_app.rb index cbcd365..67cd2d7 100644 --- a/lib/generators/rambulance/templates/exceptions_app.rb +++ b/lib/generators/rambulance/templates/exceptions_app.rb @@ -11,6 +11,6 @@ def internal_server_error def not_found end - def unprocessable_entity + def unprocessable_content end end diff --git a/lib/generators/rambulance/templates/rambulance.rb b/lib/generators/rambulance/templates/rambulance.rb index b2dfaee..4c857cd 100644 --- a/lib/generators/rambulance/templates/rambulance.rb +++ b/lib/generators/rambulance/templates/rambulance.rb @@ -15,7 +15,7 @@ # If Rambulance receives an exception that is not listed here, it'll render # the internal server error template and return 500 as http status. config.rescue_responses = { - # "ActiveRecord::RecordNotUnique" => :unprocessable_entity, + # "ActiveRecord::RecordNotUnique" => :unprocessable_content, # "CanCan::AccessDenied" => :forbidden, # "Pundit::NotAuthorizedError" => :forbidden, # "YourCustomException" => :not_found diff --git a/lib/generators/rambulance/templates/views/unprocessable_entity.html.erb b/lib/generators/rambulance/templates/views/unprocessable_content.html.erb similarity index 94% rename from lib/generators/rambulance/templates/views/unprocessable_entity.html.erb rename to lib/generators/rambulance/templates/views/unprocessable_content.html.erb index 88f384e..d10ba45 100644 --- a/lib/generators/rambulance/templates/views/unprocessable_entity.html.erb +++ b/lib/generators/rambulance/templates/views/unprocessable_content.html.erb @@ -49,7 +49,7 @@ } - +

The change you wanted was rejected.

diff --git a/lib/generators/rambulance/templates/views/unprocessable_entity.json.jbuilder b/lib/generators/rambulance/templates/views/unprocessable_content.json.jbuilder similarity index 100% rename from lib/generators/rambulance/templates/views/unprocessable_entity.json.jbuilder rename to lib/generators/rambulance/templates/views/unprocessable_content.json.jbuilder diff --git a/lib/rambulance/exceptions_app.rb b/lib/rambulance/exceptions_app.rb index ff5066b..1595387 100644 --- a/lib/rambulance/exceptions_app.rb +++ b/lib/rambulance/exceptions_app.rb @@ -38,7 +38,11 @@ def self.call(env) end end - action(status_in_words).call(env) + if exception.nil? && status_in_words == :unprocessable_entity + [302, { "Location" => "/rambulance/unprocessable_content" }, [""]] + else + action(status_in_words).call(env) + end end def self.local_prefixes @@ -53,6 +57,12 @@ def #{status_in_words} ACTION end + def unprocessable_entity + unprocessable_content_path = error_path(:unprocessable_content) + + render(template_exists?(unprocessable_content_path) ? unprocessable_content_path : error_path(:internal_server_error)) + end + def process(action, *args) if action.to_s.empty? action = request.env["PATH_INFO"][1..-1].to_sym diff --git a/test/exceptions_app_test.rb b/test/exceptions_app_test.rb index b9a85fc..fa523ed 100644 --- a/test/exceptions_app_test.rb +++ b/test/exceptions_app_test.rb @@ -6,4 +6,10 @@ class ExeptionsAppTest < ActionDispatch::IntegrationTest assert_equal 404, response.status end + + test 'returns 302 for unprocessable_entity' do + get '/rambulance/unprocessable_entity', headers: { 'Accept' => '*/*' } + + assert_redirected_to '/rambulance/unprocessable_content' + end end