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

[QUESTION] What's the best way to handle errors between apps? #17

Open
frabraga opened this issue Mar 3, 2020 · 3 comments
Open

[QUESTION] What's the best way to handle errors between apps? #17

frabraga opened this issue Mar 3, 2020 · 3 comments

Comments

@frabraga
Copy link

frabraga commented Mar 3, 2020

Considering the example:

# user/internal.py
from .services import UserService

def get_user():
    try:
        UserService.get_user()
    except SomeIntegrationError:
        # Do something
# artists/interfaces.py
from user.apis import UserAPI

def get_user():
    return UserAPI.get_user()

What do you guys think it's the best way to handle this situation?

I'm thinking of just raise an exception on internal.py and ignore that we should return a Json or we could return a dict with an error key and check on interfaces if that key exists and then handle the error there. But I don't like either option.

@phalt
Copy link
Owner

phalt commented Mar 3, 2020

This is my immediate first thought:

  1. I would make UserAPI.get_user() raise a generic, but useful error:
def get_user():
    try:
        UserService.get_user()
    except SomeIntegrationError:
       raise UserError("A useful error message")
  1. If the artists domain needed to handle the errors for their own case, I would also make the interface capture the error and handle it how it wants:
from user.apis import UserAPI, UserError

def get_user():
    try:
        return UserAPI.get_user()
    except UserError:
        # Handle case specially for artists

As I said - they are my immediate thoughts, and might not prove useful.

I think returning an actual Exception if it is a software API is appropriate (I.e. - python code calling python code).
If it is an HTTP API, then UserAPI.get_user should return an object with a defined error format that artist/interfaces can look out for:

from user.apis import UserAPI

def get_user():
    response = UserAPI.get_user()
    if response.get('errors'):
        # Handle errors in API response
    return response
@frabraga
Copy link
Author

frabraga commented Mar 4, 2020

Thanks for your answer @phalt.

After thinking more about it, the Exception approach does make more sense to us and we'll go with it.

Thanks again!

@phalt
Copy link
Owner

phalt commented Mar 5, 2020

Thanks for your input @frabraga - you're helping to make this styleguide better!

Let me know if I can help any other way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants