Skip to content

Fix: Admin visiting non-existent enterprise raises error #13224

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

Conversation

Tresor11
Copy link
Contributor

@Tresor11 Tresor11 commented Mar 21, 2025

What? Why?

Currently, when an admin tries to edit a no-existing enterprise, a NoMethodError is raised.

This commit adds a require_enterprise method that raises a ActiveRecord::RecordNotFound error when the enterprise is not found and redirects the user to the enterprise index page with an error message.

What should we test?

  • Login then visit /admin/enterprises/no-extisting/edit you should be redirected to the enterprises path with an error message
Screenshot 2025-04-01 at 02 05 56

Release notes

Changelog Category (reviewers may add a label for the release notes):

  • User facing changes

Currently when an admin tries to edit an no-existing enterprise, a
NoMethodError is raised.

This commit adds a set_enterprise setter method to the
enterprises controller that sets the @enterprise instance variable to
have the same value as the enterprise object defined in the the edit
method; this method also rescues the NotFound error  in case
the enterprise is not found and redirects the user to the enterprises
index page with a error message.
@github-project-automation github-project-automation bot moved this to All the things 💤 in OFN Delivery board Mar 21, 2025
@sigmundpetersen sigmundpetersen moved this from All the things 💤 to Code review 🔎 in OFN Delivery board Mar 25, 2025
@rioug rioug added the user facing changes Thes pull requests affect the user experience label Mar 25, 2025
Copy link
Collaborator

@rioug rioug left a comment

Choose a reason for hiding this comment

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

Thanks @Tresor11 🙏
Your solution does fix the problem, but looking at the code I can't tell where this @enterprise is coming from. I wonder if the better fix would be to replace the @enterprise by @object, which represent the model we are looking at. It should be loaded properly via Admin::ResourceController Let's see what other reviewers think.

@dacook dacook self-requested a review March 25, 2025 23:30
Copy link
Member

@dacook dacook left a comment

Choose a reason for hiding this comment

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

Thanks for looking into this. It appears that it solves the issue, but it would be good to add a request spec to clarify exactly what it solves.

From what I can see, this change shouldn't be necessary. The EnterprisesController#load_methods_and_fees method is expected to run after ResourceController#load_resource. load_resource calls the ActiveRecord find method, which should raise an exception if the resource (enterprise) is not found (see https://github.com/dacook/openfoodnetwork/blob/master/app/controllers/admin/resource_controller.rb#L153-L159).

The ResourceController is intended to handle that and redirect you to the index page, just as your fix does (see resource_not_found).

So it would be good to investigate why that functionality wasn't working in this case. The best way would be to write a request spec as mentioned, then you can poke and prod (eg with binding.pry) to see what's happening.

Would you be able to have a go at that?

@github-project-automation github-project-automation bot moved this from Code review 🔎 to In Progress ⚙ in OFN Delivery board Mar 25, 2025
@Tresor11 Tresor11 force-pushed the tb-rescue-not-found-error-for-enterprise branch 2 times, most recently from c8ecf63 to 72c855e Compare March 31, 2025 19:26
This commit updates the method name to be called required_enterprise
since we only expect it to raise an error when the enterprise is not found.
@Tresor11 Tresor11 force-pushed the tb-rescue-not-found-error-for-enterprise branch from 72c855e to 507705a Compare March 31, 2025 19:28
@Tresor11
Copy link
Contributor Author

Thanks for the review. @dacook @rioug

I agree that reassigning the enterprise instance doesn't feel quite right, since we are already handling the ActiveRecord::RecordNotFound error in ResourceController, require_enterprise should only be responsible for raising an error when the enterprise is not found.
I've added a spec to test this behavior; i'm still not sure why this is not being raised by the resource controller

Copy link
Member

@dacook dacook left a comment

Choose a reason for hiding this comment

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

Great, thanks for adding the spec! I checked out the spec on master and could see the spec failure, then can see your change fixes it.
I've just added a commit to fix the linter errors and I think it's good 👍

@dacook dacook moved this from In Progress ⚙ to Code review 🔎 in OFN Delivery board Mar 31, 2025
@dacook dacook requested a review from rioug March 31, 2025 22:32
Copy link
Collaborator

@rioug rioug left a comment

Choose a reason for hiding this comment

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

Looks good ! Thanks @Tresor11 🙏

@rioug rioug moved this from Code review 🔎 to Test Ready 🧪 in OFN Delivery board Mar 31, 2025
@filipefurtad0 filipefurtad0 self-assigned this Apr 1, 2025
@filipefurtad0 filipefurtad0 added pr-staged-au staging.openfoodnetwork.org.au and removed pr-staged-au staging.openfoodnetwork.org.au labels Apr 1, 2025
@filipefurtad0
Copy link
Contributor

filipefurtad0 commented Apr 1, 2025

Hey @Tresor11, welcome 💐

Many thanks for fixing this one.

I've reproduced the issue, by logging in as an admin and visiting the enterprise admin path, with a non-existing enterprise name, which triggered the error 500 you described.

After staging the PR, repeating the same scenario displayed the error message:

image

The translation is being picked up correctly - repeating the steps before, with FR as a locale:

image

Looks great! Merging 🎉

@filipefurtad0 filipefurtad0 merged commit bb527a2 into openfoodfoundation:master Apr 1, 2025
35 checks passed
@github-project-automation github-project-automation bot moved this from Test Ready 🧪 to Done in OFN Delivery board Apr 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
user facing changes Thes pull requests affect the user experience
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Admin visiting non-existent enterprise raises error
4 participants