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

3688 error handling #3702

Merged
merged 26 commits into from
Jul 10, 2023
Merged

3688 error handling #3702

merged 26 commits into from
Jul 10, 2023

Conversation

jadekstewart3
Copy link
Contributor

Checklist:

[x]I have performed a self-review of my own code,
[x]I have commented my code, particularly in hard-to-understand areas,
[x]I have made corresponding changes to the documentation,
[x]I have added tests that prove my fix is effective or that my feature works,
[x]New and existing unit tests pass locally with my changes ("bundle exec rake"),

Resolves #3688

Description

Re-worked error message related to Storage Location, and refactored error handling into a private method with a custom error message to clearly convey to the user what the error is. Re-labeled the 'From Storage Location' field to 'Storage location', and re-named the error associated with the field.

List any dependencies that are required for this change. (gems, js libraries, etc.)

Include anything else we should know about. -->

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Tested in the audit_request_spec.rb This test ensures that the correct template is rendered if no Storage location is selected, and that the flash message is rendering the custom error message.

Provide instructions so we can reproduce.
Do we need to do anything else to verify your changes?
If so, provide instructions (including any relevant configuration) so that we can reproduce? -->

To reproduce the rendered error messages. Log in to the application as a Bank User - Organization Admin, begin a new audit without selecting a storage location, save the audit, and the custom error messages will be displayed.

Screenshots

Screenshot 2023-06-30 at 10 11 02 AM

@jadekstewart3 jadekstewart3 changed the title 3688 error handling [WIP]3688 error handling Jun 30, 2023
@jadekstewart3 jadekstewart3 changed the title [WIP]3688 error handling 3688 error handling Jun 30, 2023
app/controllers/audits_controller.rb Outdated Show resolved Hide resolved
app/views/storage_locations/_source.html.erb Outdated Show resolved Hide resolved
@jadekstewart3 jadekstewart3 changed the title 3688 error handling [WIP] 3688 error handling Jun 30, 2023
@jadekstewart3 jadekstewart3 requested a review from dorner July 6, 2023 00:34
@jadekstewart3 jadekstewart3 changed the title [WIP] 3688 error handling 3688 error handling Jul 6, 2023
Copy link
Collaborator

@dorner dorner left a comment

Choose a reason for hiding this comment

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

One comment/question.

@cielf the label for the actual field has changed - are you OK with this?

def handle_audit_errors
if @audit.errors.present?
errors = @audit.errors.collect { |error| "#{error.attribute.capitalize} ".tr("_", " ") + error.message }
flash[:error] = errors[1]
Copy link
Collaborator

Choose a reason for hiding this comment

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

This smells like a magic number - why are we getting only the second error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I grabbed the second one because that error message was the closest to what @cielf had said in the ticket that it should say. I am happy to display both of them if you would prefer.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Relying on the order of errors is incredibly fragile. If you are trying to condense two similar errors into one, you might want to actually examine the strings for sentinel values and remove one if you find two of them.

Copy link
Contributor Author

@jadekstewart3 jadekstewart3 Jul 6, 2023

Choose a reason for hiding this comment

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

@dorner Heard! No problem 😉 Is this more like what you would like to see?

Screenshot 2023-07-06 at 3 14 59 PM

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm... not quite. What this is doing is picking out one specific error message (one that has "blank" in it) and only showing it if it exists, and swallowing all other messages.

I was thinking something like:

flash[:error] = @audit.errors.uniq(&:attribute).map(&:message)

That should make it so that you only get one error message per attribute.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahhhh! okay! I see! I was able to implement your suggestion while displaying the attribute of the error with the error message.

Screenshot 2023-07-07 at 2 53 36 PM

@jadekstewart3 jadekstewart3 requested a review from dorner July 6, 2023 22:22
Copy link
Collaborator

@dorner dorner left a comment

Choose a reason for hiding this comment

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

One last minor quibble, then it's ready to merge!

it "re-renders the 'new' template with an error message when an invalid storage location is given" do
post audits_path(default_params.merge(audit: invalid_storage_location_attributes))
expect(response).to render_template(:new)
expect(flash[:error]).to_not eq("Storage location can't be blank")
Copy link
Collaborator

Choose a reason for hiding this comment

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

This line isn't necessary - if something is equal to X (next line) by definition it isn't equal to anything but X 😁

@dorner
Copy link
Collaborator

dorner commented Jul 10, 2023

Nice, thanks!

@dorner dorner merged commit d598825 into rubyforgood:main Jul 10, 2023
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.

Error for no storage location on new audit should refer to audit not transfer.
2 participants