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

LF-4092 - update animal controllers to use handle objection error #3148

Merged
merged 8 commits into from
Mar 18, 2024

Conversation

Duncan-Brain
Copy link
Collaborator

@Duncan-Brain Duncan-Brain commented Mar 1, 2024

Description

On creation of the objection error handler it was mentioned by @SayakaOno that we should update all animal controllers to be consistent. (and a modest -50 lines of code ;) )

Now every endpoint in animals with a trx and excess checks should be covered.

Also added in a test that was missing from batch and controller -- should not be able to add a default breed with a custom type.

To test: For exhaustive testing remove one check at a time and check that it is still handled.

Jira link: LF-4092

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Passes test case
  • UI components visually reviewed on desktop view
  • UI components visually reviewed on mobile view
  • Other (please explain)

Checklist:

  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • The precommit and linting ran successfully
  • I have added or updated language tags for text that's part of the UI
  • I have added "MISSING" for all new language tags to languages I don't speak
  • I have added the GNU General Public License to all new files

@Duncan-Brain Duncan-Brain requested review from a team as code owners March 1, 2024 23:15
@Duncan-Brain Duncan-Brain requested review from antsgar and SayakaOno and removed request for a team March 1, 2024 23:15
@Duncan-Brain Duncan-Brain self-assigned this Mar 4, 2024
Copy link
Collaborator

@SayakaOno SayakaOno left a comment

Choose a reason for hiding this comment

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

Thank you for working on this!
I prefer receiving 400 rather than 500 when possible. I would like to hear what others think!

Comment on lines -54 to -57
if (!Array.isArray(req.body)) {
await trx.rollback();
return res.status(400).send('Request body should be an array');
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

In my humble opinion, this is nicer than an Unknown internal server error. You haven't removed the one from animalBatchController!

Copy link
Collaborator Author

@Duncan-Brain Duncan-Brain Mar 6, 2024

Choose a reason for hiding this comment

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

Its more explicit yeah, for me I prefer to not feel like I have to handle every case where there should be an array provided since it could add up to a lot of code.

The message provided by objections error "req.body is not iterable" makes enough sense to me. But I also think if you want to make a new Error Type in the future that makes this generic error more readable -- that is nice too!

Comment on lines -65 to -68
if (!Array.isArray(related_animal_ids) || !Array.isArray(related_batch_ids)) {
await trx.rollback();
return res.status(400).send('Animal ids and batch ids must be arrays');
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I prefer keeping this too!

@Duncan-Brain
Copy link
Collaborator Author

I agree it is nice to receive 400 where possible. But to me the solution is to create handled error code types. And continue to allow "unknowns" to be 500.

Let the bad 500 be incentive to make more error codes haha

@antsgar
Copy link
Collaborator

antsgar commented Mar 7, 2024

@Duncan-Brain is there a way we could add more cases to the handle error function so that what is currently returned as a 400 would still be returned as a 400?

@Duncan-Brain
Copy link
Collaborator Author

Duncan-Brain commented Mar 7, 2024

@SayakaOno @antsgar I added a custom test for the Array check .. I don't really love the solution I came up with since it seems a little messy, and the error message is not the nicest of things... but check it out maybe you think it is ok.

One thing that came up from doing this:

In our controllers we use the Models to write to the DB, and in our tests we use plain Knex. By not testing our controller during tests this creates two different error messages. Handled in this case, but reasonable to think we will forget and cause some problems down the line... I think we should be testing our API through our controllers and not knex directly, especially since we have been putting more constraint and response logic in the Model directly -- and thats how we use our api so is a better test for what we expect to happen.

Also there was a test in animal_groups being flakey (the order it was returned was not the same as it was added in test) so I made a little band-aid there.

Copy link
Collaborator

@SayakaOno SayakaOno 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 open to approval, but I'm still in favour of finding a better way of validating the request body that Duncan will feel comfortable with rather than letting the handleCustomError function handle internal errors :)

packages/api/tests/animal_group.test.js Outdated Show resolved Hide resolved
@Duncan-Brain
Copy link
Collaborator Author

I can put all the Array checking back in if that is what is preferred by others too. Maybe we can discuss in tech daily... I definitely agree it is not as nice looking as is.

I think when we add typescript to the backend we could enforce types that way a lot easier and the returned errors would be more parseable or automatic. We just don't have that right now.

For me the reason why I do not personally want to make it nicer now is that:

  • Error is not translated so it is not really user facing -- and so it is only useful for us the developers.
  • The burden (lines of code) of enforcing every type, for every variable, on every route seems high and the value of letting a generic error handle it seems useful to me.
  • I guess I see it that creating a custom error message for related_animal_ids = ARRAY is no more important than creating a custom error message for breed = STRING (putting "breed": 5 in your body will throw an objection error that unless you know what it means it might not make sense either).

SayakaOno
SayakaOno previously approved these changes Mar 8, 2024
const isNotIterable = 'is not iterable';
// Plain knex response for tests
const isNotIterableTests =
'Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.';
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 a fan of this parsing of specific error messages tbh, it seems brittle -- if they change the message in the slightest this will break. Out of all the options, I think I'd rather just keep the 500 or keep the explicit checks for 400s. Thoughts?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I am personally for keeping the 500 until we add typescript to backend or some other request shape checker.

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 okay with that, prefer it to this last approach!

Copy link
Collaborator Author

@Duncan-Brain Duncan-Brain Mar 13, 2024

Choose a reason for hiding this comment

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

Last approach it is! lol

@antsgar should I make a tech debt ticket (spike, rfc) about specifying our backend request shape? There has been a few suggestions in the past about improving our backend in this regard and we have OAS stuff but to my knowledge we haven't chosen a direction for this improvement yet.

@antsgar antsgar added this pull request to the merge queue Mar 18, 2024
Merged via the queue into integration with commit 6df88d4 Mar 18, 2024
5 checks passed
@antsgar antsgar deleted the LF-4092-add-error-handler-to-animal-endpoints branch March 18, 2024 14:59
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