-
Notifications
You must be signed in to change notification settings - Fork 84
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
LF-4092 - update animal controllers to use handle objection error #3148
Conversation
There was a problem hiding this 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!
if (!Array.isArray(req.body)) { | ||
await trx.rollback(); | ||
return res.status(400).send('Request body should be an array'); | ||
} |
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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!
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'); | ||
} |
There was a problem hiding this comment.
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!
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 |
@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? |
@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. |
There was a problem hiding this 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 :)
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:
|
packages/api/src/util/errorCodes.js
Outdated
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.'; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.
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
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
Checklist: