Skip to content

Commit

Permalink
Keep the compatibility with Rack
Browse files Browse the repository at this point in the history
  • Loading branch information
yuki24 committed Aug 4, 2024
1 parent c383e5a commit a528990
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/rambulance/templates/exceptions_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ def internal_server_error
def not_found
end

def unprocessable_entity
def unprocessable_content
end
end
2 changes: 1 addition & 1 deletion lib/generators/rambulance/templates/rambulance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
}
</style>

<!-- This file lives in app/views/errors/unprocessable_entity.html.erb -->
<!-- This file lives in app/views/errors/unprocessable_content.html.erb -->
<div class="dialog">
<div>
<h1>The change you wanted was rejected.</h1>
Expand Down
12 changes: 11 additions & 1 deletion lib/rambulance/exceptions_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions test/exceptions_app_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a528990

Please sign in to comment.