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

Guidance Needed for Handling One-Time Errors. #160

Open
aparreno opened this issue Nov 23, 2024 · 1 comment
Open

Guidance Needed for Handling One-Time Errors. #160

aparreno opened this issue Nov 23, 2024 · 1 comment

Comments

@aparreno
Copy link

aparreno commented Nov 23, 2024

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?
@RadoRado
Copy link
Member

RadoRado commented Dec 2, 2024

@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:

  1. 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.
  2. If this is going to be an occurring pattern, I'd move to the custom error handler of DRF

Let me know if this makes sense to you 👍

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

No branches or pull requests

2 participants