dry-python/returns

Use with Sentry

filwaline opened this issue · 2 comments

I am very interested in this library and have read some related blogs, such as https://sobolevn.me/2019/02/python-exceptions-considered-an-antipattern. If I want to use returns with Sentry, do you have any ideas on how to do it? Should I wrap everything with Result (@safe everywhere) and handle them until they return to the entrypoint, using .alt report to Sentry?

For example:

def some_complex_compute(data:int) -> ResultE[int]:
    ...

def entrypoint():
    res = some_complex_compute(0)
    fin = res.alt(sentryable)
    return fin

Or should I just leave some exceptions raised so that Sentry can catch them and report?

That's an interesting question! It depends on how your application is structured, for an Web API for example you could have an interceptor/middleware to check that and if there's an exception and then make what you want with that info. But in your case it's something that's expected by a third party code where you don't have the ability to extend/modify it so just throw the exception at the end of our program!

from returns.functions import raise_exception
from returns.result import ResultE

def some_complex_compute(data:int) -> ResultE[int]:
    ...

def entrypoint() -> int:
    return (
        some_complex_compute(0)
        .alt(raise_exception)
        .unwrap()
    )

Closing issue @filwaline, feel free to open it again if you consider the question is not answered!