Use of panic on library development

A while back I explored customerrors a little bit in this topic Custom errors with payloads (with current zig), however I don’t think it mixes well with normal zig code.

I think the current best way, is to do what @andrewrk suggested here.

Because it can simply be used with zigs error unions.
If you are interested to get more information about the error, the user at the call site uses a config parameter to pass a pointer to a struct you place at the call site, this struct then gets filled by the called function.

This would also allow you to use this extra information to write a wrapper function that panics with that information, in cases where you don’t want to try to handle the error.
Maybe you could even pack both into one function if you calculate the return type based on the config and use clever helper inline functions, but I think that might over complicate things. Just writing the wrapper that panics when you need one seems simpler.

3 Likes