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 about working across modules #5

Open
dineshbvadhia opened this issue Jun 16, 2024 · 3 comments
Open

Question about working across modules #5

dineshbvadhia opened this issue Jun 16, 2024 · 3 comments
Labels
question Further information is requested

Comments

@dineshbvadhia
Copy link

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
Copy link
Owner

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).

@alexandermalyga alexandermalyga added the question Further information is requested label Jun 16, 2024
@dineshbvadhia
Copy link
Author

dineshbvadhia commented Jun 16, 2024

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
Copy link
Author

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('[email protected]')
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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants