9 Likes
Proper use of recover is only for testing panic and undefined behavior runtime safety checks.
Do you have an example of how you do this?
2 Likes
Let’s say that foo(0)
is designed to panic.
To test that the function panics you can:
test "foo(0) panics" {
const err = recover.call(foo, .{0}, error.Panic);
std.testing.expectError(error.Panic, err).
}
For this to work, you need a custom test_runner with a panic handler:
pub fn panic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace, ret_addr: ?usize) noreturn {
recover.panicked();
std.builtin.default_panic(msg, error_return_trace, ret_addr);
}
4 Likes
I see. Thanks, that’s a nice use case.
zig-recover version 1.0.0 released for zig 0.12.0 or later
To fetch as a dependency in your build.zig.zon
:
zig fetch --save https://github.com/dimdin/zig-recover/archive/refs/tags/1.0.0.tar.gz
Usage is simplified:
const recover = @import("recover");
pub const panic = recover.panic;
fn function() void {
unreachable;
}
pub fn main() !void {
recover.call(function, .{}) catch {};
// flow continues here because recover.call recovers
// from unreachable and returns error.Panic
}
See the project README.md for more information.
5 Likes