-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[#162105] Handle mime type errors #4819
base: master
Are you sure you want to change the base?
Conversation
29d7a11
to
15e7da3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not seeing how exceptions are mapped to errors for environments other than test. It would be nice to have a single solution for all environments btw
I might be wrong but I think we are still not catching the mime type error with this approach
config/environments/development.rb
Outdated
@@ -12,7 +12,7 @@ | |||
config.eager_load = true | |||
|
|||
# Show full error reports. | |||
config.consider_all_requests_local = true | |||
config.consider_all_requests_local = false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we leave this flag on? it's useful to have this in development
def render_acting_error | ||
render "/acting_error", status: 403, layout: "application", formats: formats_with_html_fallback | ||
rescue_from NUCore::PermissionDenied, CanCan::AccessDenied, with: :render_403 | ||
rescue_from NUCore::NotPermittedWhileActingAs, with: :render_acting_error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should leave this rescue_from for all environments
7e06dd0
to
b88c6c1
Compare
@@ -208,4 +193,23 @@ def formats_with_html_fallback | |||
request.formats.map(&:ref) + [:html] | |||
end | |||
|
|||
def render_403(_exception) | |||
byebug |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this should be here - do we have a spec that would catch something like this?
rescue_from NUCore::NotPermittedWhileActingAs, with: :render_acting_error | ||
|
||
# Test exception handlers | ||
if Rails.env.test? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way to test without this? I usually try to avoid changing the application code to make testing work
b88c6c1
to
e7af96c
Compare
end | ||
|
||
def call(env) | ||
env["HTTP_ACCEPT"] = "*/*" if env["HTTP_ACCEPT"] =~ /(\.\.|{|})/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should let rails decide which types are invalid and set the mime type to text/html
as fallback, like in the fallback_to_html_format_if_invalid_mime_type
function here: https://guides.rubyonrails.org/configuring.html#config-exceptions-app.
config/application.rb
Outdated
config.autoload_paths << Rails.root.join("lib/middlewares") | ||
config.eager_load_paths << Rails.root.join("lib/middlewares") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if this is needed since you're requiring the file on line 8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯
Release Notes
Created an errors controller to handle the errors in the app and set the routes to redirect the raised exceptions to the proper errors controller method and handle scenarios where requests with invalid format are made.
Additional Context
Exceptions weren't being handled properly while running the tests, so I kept the error handling in the application controller for the tests. Any suggestion on how to capture exceptions through the routes in the test environment is appreciated.