alexandermalyga / poltergeist

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

Unexpected errors #4

Closed dineshbvadhia closed 3 months ago

dineshbvadhia commented 3 months ago

This is not an Issue but two related usage questions. For my system, it is necessary to capture unexpected errors as exceptions. These should bubble up and be logged (with structlog) and the system will exit. How does poltergeist handle unexpected errors in a general way?

Secondly, in the examples, say:

@catch(OSError)
def read_text(path: str) -> str:
    with open(path) as f:
        return f.read()

The expected error is stated as OSError. But what if, the expected error is not known in advance (or alternatively, the expected errors are left out deliberately to see what errors are generated)?

alexandermalyga commented 3 months ago

Hey! You should handle errors using standard try-except in any place where it's suitable for your application, then you can wrap the raised exception in an Err() instance and return it.

The @catch decorator does this for you as a convenience feature, if you'd like to handle exceptions of any type then you can use @catch(Exception) or even @catch(BaseException), although the latter is usually not recommended.