What's the idiomatic way to handle errors?

There are a number of small libs I’ve made in haxe (haven’t added to haxelib yet but will) and one thing that always trips me up more than the coding itself is deciding what to do about bad input or errors. My goal is for these libs to be of course usable in haxe but I also want people using javascript, python and so on to also be able to use the output. Is there a standard haxian way to handle errors? Are exceptions generally what’s recommended? I don’t like how exceptions work in C++ but if users of haxe libs are expecting the lib to use exceptions, then I’ll go with that. But I’m also tempted to make return types nullable and trace errors because for me personally exceptions catch me off guard when they happen. In python I’m in the habit of putting things in try/catch blocks because it’s common there. But that’s not how I’d do things in a lot of other languages.

What is most in accordance with the principle of least surprise for a haxe library?

I would vote for Exceptions.

Beware, you can “throw” anything in haxe. Often, Std lib just throws string messages.

If you want to handle more complex exceptions ( with error codes + messages …etc ) , I recommend Tink Error ( GitHub - haxetink/tink_core: Core utilities )

1 Like

I made a short example page about Tink Errors : tinkerrors [Sugoï, a web framework for Haxe]