Closed jawrainey closed 3 years ago
I've implemented a proof-of-concept for (2) in this branch. The advantage here is that we do not have to use try/catch in all services for all possible requests errors. What's your thoughts on this approach @davidverweij?
To test it:
poetry run consumer
http://0.0.0.0:8000/inventory/device/history/ABC
This means that when a 500 error occurs (that we do not account for) then we can provide an enveloped reponse to clients. For example, modify the url above to be "https://fake.abc" and that the response is enveloped (data/meta) rather than "internal server error". As long as we log this data we can address any internal problems.
Tested it - works well! What do you think about feeding back some expected errors. For example - if a dependency if broken (i.e. the inventory system), it can make sense to just actually throw the HTTP 500
to reflect the API in a more honest manner. Enveloping everything does convolute some things a bit; i.e., depending on who is using it. But due to our API mainly being an (project) internal approach, your solution works well and keeps the API running regardless of dependencies!
So, LGTM
We return a status_code
of 500 if an error is thrown internally but in a way that allows clients to consumer the API consistently. As we use "meta.success" to outline as source of truth then the client can use either that or the http.status_code if an error is thrown.
It is possible for an error to be raised in consumer.services.inventory:response such as if the inventory is offline, i.e., all
requests.HTTPError
. This is becauseres.raise_for_status()
is not implemented and not try/catched, and would be the same in other services, support, UCAM, etc.We have two options:
raise_for_status
in a try/catch forHTTPError
then log the error.