alexandermalyga / poltergeist

Rust-like error handling in Python, with type-safety in mind.
MIT License
121 stars 3 forks source link

Question about working across modules #5

Open dineshbvadhia opened 3 months ago

dineshbvadhia commented 3 months ago

When working across modules for example, in these 3 modules - this.py, that.py and other.py - is the @catch decorater placed on def a() only or def a(), def b() and def c()?

# other.py
def c()
    return c()

# that.py
import other

def b():
    return other.b()

# this.py
import that

def a():
    d, e, f = that.b()
alexandermalyga commented 3 months ago

It's up to you, depends on where you want to start having functions returning Result instead of raising exceptions. The function you choose will stop bubbling up exceptions and will return Err instead (or Ok if no exception was raised).

dineshbvadhia commented 3 months ago

Apologies for the pedantic questions but to adopt poltergeist a lot of my code base will need to be modified. Say, I want the exception to bubble up to the original call ie.

# other.py
def c()
    return c()

# that.py
import other

def b():
    return other.b()

# this.py
import that

@catch(Exception)
def a():
    d, e, f = that.b()

result = a()

Will an exception in either def b() or def c() bubble up as-is or do they need try/except Exception blocks? ie.

# other.py
def c()
    try:
        return c()
    except Exception as exc:
        raise exc
dineshbvadhia commented 3 months ago

On the rustedpy/result github page there is this example (https://github.com/rustedpy/result#summary) which is then shown coded with result:

def get_user_by_email(email: str) -> Tuple[Optional[User], Optional[str]]:
    """
    Return the user instance or an error message.
    """
    if not user_exists(email):
        return None, 'User does not exist'
    if not user_active(email):
        return None, 'User is inactive'
    user = get_user(email)
    return user, None

user, reason = get_user_by_email('ueli@example.com')
if user is None:
    raise RuntimeError('Could not fetch user: %s' % reason)
else:
    do_something(user)

How would this example be coded with poltergeist @catch?