Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

diebas
Copy link
Collaborator

@diebas diebas commented Dec 6, 2024

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.

@diebas diebas force-pushed the 162105-handle-mime-type-error branch 6 times, most recently from 29d7a11 to 15e7da3 Compare December 11, 2024 14:55
@diebas diebas marked this pull request as ready for review December 11, 2024 15:03
Copy link
Collaborator

@joaquinco joaquinco left a 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

@@ -12,7 +12,7 @@
config.eager_load = true

# Show full error reports.
config.consider_all_requests_local = true
config.consider_all_requests_local = false
Copy link
Collaborator

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
Copy link
Collaborator

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

@diebas diebas force-pushed the 162105-handle-mime-type-error branch from 7e06dd0 to b88c6c1 Compare December 12, 2024 18:15
@@ -208,4 +193,23 @@ def formats_with_html_fallback
request.formats.map(&:ref) + [:html]
end

def render_403(_exception)
byebug
Copy link
Collaborator

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?
Copy link
Collaborator

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

@diebas diebas force-pushed the 162105-handle-mime-type-error branch from b88c6c1 to e7af96c Compare December 16, 2024 17:39
end

def call(env)
env["HTTP_ACCEPT"] = "*/*" if env["HTTP_ACCEPT"] =~ /(\.\.|{|})/
Copy link
Collaborator

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.

Comment on lines 58 to 59
config.autoload_paths << Rails.root.join("lib/middlewares")
config.eager_load_paths << Rails.root.join("lib/middlewares")
Copy link
Collaborator

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

@diebas diebas requested a review from joaquinco December 23, 2024 17:44
Copy link
Collaborator

@joaquinco joaquinco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants