You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi! I'm new to the Django Styleguide and looking to implement its recommendations in my DRF project. However, I'm unsure about how to handle one-time errors. For example, consider the following case: response = make_req({"cars_urls": urls, "user_id": created_id.id}) if response.status_code != 200: return Response( {"error": "Failed to fetch the cars"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR, )
My question is:
Should this type of error handling be managed in the custom_exception_handler, potentially by raising a custom exception in the service layer and catching it there?
Or is it better to handle such errors directly in the service layer using a try-except block and returning a Response object from the view?
The text was updated successfully, but these errors were encountered:
@aparreno Hello & thank you for opening this issue 👋
To be honest, I'm a bit on the fence about this. Only yesterday I had to decide the exact same thing - how to return a one-time error?
My decision was to just return a Response, doing the one-time handling, without any message.
Yet, if I have to do it again in a different place, I'll most probably move this to the custom error handling with a proper exception class raised for that 👍
Generally speaking, if you extract the error formatting in a way that can be reused for returning a Response, then, you should be able to do ad-hoc decisions.
So, to summarize:
If this is just a one-time thing and no-one is actually going to be handling that error in a programatic way, what you've done is good-enough.
If this is going to be an occurring pattern, I'd move to the custom error handler of DRF
Hi! I'm new to the Django Styleguide and looking to implement its recommendations in my DRF project. However, I'm unsure about how to handle one-time errors. For example, consider the following case:
response = make_req({"cars_urls": urls, "user_id": created_id.id})
if response.status_code != 200:
return Response( {"error": "Failed to fetch the cars"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR, )
My question is:
The text was updated successfully, but these errors were encountered: