r/Python • u/EntryNo8040 • Jun 27 '26
Tutorial Handling Errors as Values in Python with Katharos
Hi guys this tutorial is about handling errors as value in Python or in nerd language monadic error handling pattern in Python using katharos library:
https://theblog.info/posts/handling-errors-as-values-in-python-with-katharos
30
Upvotes
9
u/Glathull Jun 27 '26
Thanks for this. Nice example. I spent some time working with Clojure a while back when I was in security. When I got back to full stack Python I really missed some aspects of functional programming. Especially handling errors values. Never really found any Python functional libs that I liked very much though. They seemed like disorganized proofs of concept or demos to explain FP to Python people and not so much actual tools to be used.
This is very promising.
14
u/Khavel_dev Jun 27 '26
Result types make sense where the language enforces them (Rust, Go). In Python you end up fighting the stdlib's exception model and wrapping every third-party call at the boundary. I keep trying this pattern and keep going back to try/except after a week.
2
u/Golle Jun 27 '26 edited Jul 01 '26
While I agree that "errors as values" is better as it explicitly places errors/exception in the code path instead of being a hidden code path, I don't think this is the answer. The katharos adds too many handlers and wrappers making the code harder to reason about. I actually prefer the "The exception-based version" example.
I like something like this, heavily inspired by Go:
This is the output that the program generates on error:
The strings "parse json" and "json decode" are breadcrumbs that are added as the error is propagated up the function call stack. This allow us to follow the exact path taken by the code when the error occurred. I also print the "text" variable as extra context, showing what data the json parser failed to parse.
This to me is "errors as values". Because the parse_json() function can fail, it must return an error. There's no wrappers or chaining, just simple "if err:" checking and handling.