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

Refactor error handling #4856

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

Refactor error handling #4856

wants to merge 30 commits into from

Conversation

diebas
Copy link
Collaborator

@diebas diebas commented Dec 16, 2024

Release Notes

Refactor for how errors are being handled in the app.

It consists of adding an error controller with a method for each error type, adding routes to derivate each exception to the respective controller method, and setting up the exception app to use the routes.

Also updated controller tests to check if exceptions are being raised instead of checking that pages are being rendered.

@diebas diebas force-pushed the refactor-error-handling branch 3 times, most recently from 4b0e849 to 6df5639 Compare December 20, 2024 13:53
@diebas diebas force-pushed the refactor-error-handling branch from 6df5639 to 90d4c13 Compare December 23, 2024 13:57
@diebas diebas marked this pull request as ready for review December 23, 2024 20:49
Copy link
Collaborator

@LeticiaErrandonea LeticiaErrandonea left a comment

Choose a reason for hiding this comment

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

Nice job here!

Left a bunch of comments, basically the same one I copy pasted so I could point to the right lines

Comment on lines +171 to +172
def with_dropped_params(&)
QuietStrongParams.with_dropped_params(&)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Was this change automatically done by the linter?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, I didn't change this line

if current_user
render "errors/forbidden", status: :forbidden
else
redirect_to new_user_session_path, alert: "You need to login to access this page." and return
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we need to return? Is it the same as doing this?

Suggested change
redirect_to new_user_session_path, alert: "You need to login to access this page." and return
return redirect_to new_user_session_path, alert: "You need to login to access this page."

if login
it_should_require_login
else
it do
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we add some text here?

if login
it_should_require_login
else
it do
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we add some text here?

@@ -100,8 +94,7 @@ def do_request

it "does not allow the purchaser" do
sign_in purchaser
do_request
expect(response.status).to eq(403)
expect { do_request }.to raise_error(CanCan::AccessDenied)
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 use the shared example here?

@@ -58,8 +58,7 @@
visit facility_user_accounts_path("all", new_user)
expect(page).not_to have_content("Clone")

visit facility_user_clone_account_memberships_path("all", new_user)
expect(page.status_code).to eq(403)
expect { visit facility_user_clone_account_memberships_path("all", new_user) }.to raise_error(CanCan::AccessDenied)
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 use the shared example here?

end

it "does not have a remove link present" do
expect { visit facility_price_groups_path(facility) }.to raise_error(CanCan::AccessDenied)
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 use the shared example here?

@@ -80,8 +80,7 @@

it "cannot view the page" do
expect(reservation.order_detail).not_to be_problem
visit edit_problem_reservation_path(reservation)
expect(page.status_code).to eq(404)
expect { visit edit_problem_reservation_path(reservation) }.to raise_error(ActiveRecord::RecordNotFound)
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 use the shared example here?

@@ -99,8 +98,7 @@

it "cannot view the page" do
expect(reservation.order_detail).to be_problem
visit edit_problem_reservation_path(reservation)
expect(page.status_code).to eq(404)
expect { visit edit_problem_reservation_path(reservation) }.to raise_error(ActiveRecord::RecordNotFound)
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 use the shared example here?

@@ -117,8 +117,7 @@
visit sanger_sequencing_submission_path(SangerSequencing::Submission.last)
expect(page.status_code).to eq(200)

visit edit_sanger_sequencing_submission_path(SangerSequencing::Submission.last)
expect(page.status_code).to eq(404)
expect { visit edit_sanger_sequencing_submission_path(SangerSequencing::Submission.last) }.to raise_error(ActiveRecord::RecordNotFound)
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 use the shared example here?

@LeticiaErrandonea
Copy link
Collaborator

Some specs are failing 😢

@diebas diebas force-pushed the refactor-error-handling branch from 7e0dc50 to 6be2b80 Compare December 30, 2024 12:44
Copy link
Collaborator

@LeticiaErrandonea LeticiaErrandonea left a comment

Choose a reason for hiding this comment

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

LGTM! 💪

Comment on lines 105 to 109
if current_user
render "errors/forbidden", status: :forbidden
else
return redirect_to new_user_session_path, alert: "You need to login to access this page."
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

is this change necessary? It's not clear to me why if there's a current user an error is renderd and if not a redirection to login is done

@@ -80,6 +79,10 @@ class Application < Rails::Application
# Prevent invalid (usually malicious) URLs from causing exceptions/issues
config.middleware.insert 0, Rack::UTF8Sanitizer

config.action_dispatch.rescue_responses["NUCore::PermissionDenied"] = :forbidden
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 also add CanCan::AccessDenied

@diebas diebas requested a review from joaquinco January 14, 2025 17:20
@diebas diebas force-pushed the refactor-error-handling branch from dffdb67 to d0dd51f Compare January 14, 2025 20:10
@diebas diebas force-pushed the refactor-error-handling branch from 515ab8b to 4b96a5c Compare January 15, 2025 18:00
@diebas diebas requested a review from joaquinco January 15, 2025 21:24
@diebas diebas force-pushed the refactor-error-handling branch from a395609 to 6d5fcc7 Compare January 16, 2025 17:07
@@ -2,7 +2,7 @@

require "rails_helper"

RSpec.describe "Launching Kiosk View", :js, feature_setting: { kiosk_view: true, bypass_kiosk_auth: false } do
RSpec.describe "Launching Kiosk View", :js, :disable_requests_local, :show_rescuable_exceptions, feature_setting: { kiosk_view: true, bypass_kiosk_auth: false } do
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 the :show_rescuable_exceptions hook is no longer defined

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.

4 participants