Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distinguishing between different types of error in Pony?

Tags:

ponylang

Pony has a single exception value, which isn't parameterised.

Unfortunately frequently I have code which wants to throw different kinds of exception, and I need to know what they are in order to handle them properly --- for example, trivially, when halting the program it's important to give the user the right error message!

So what's the idiomatic way of doing this in Pony?

like image 497
David Given Avatar asked Dec 04 '25 15:12

David Given


1 Answers

Currently the idiomatic way would be to not use errors and instead use union types of the normal return type and the error reasons. For example, if you have a function that returns ReturnType and can error with either Error1 or Error2, your function signature can be

fun foo(): (ReturnType | Error1 | Error2)

The caller function must then match on the union to see what happened.

If you want some real-world examples, the files package of the standard library uses this idiom extensively.

Also, for completeness, an RFC on adding typed errors is currently being discussed.

like image 51
Benoit Vey Avatar answered Dec 09 '25 13:12

Benoit Vey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!