#[solved] How do I access the data from an error!value union?
1 messages ยท Page 1 of 1 (latest)
use catch
what's the syntax for it?
Typing on my phone:
const result_maybe = ....
const result = result_maybe catch {...};
defer result.destroy(); // or errdefer if lifetime is longer than this scope
function_that_returnerror() catch |err| { ... do what u want with err here }
I can't do that. You missed the check_hasError part
just put if inside catch bruh
I need to unit test it first, then clean it
I'm asking for a different thing, "bruh"
I'm asking for syntax
If you don't know it, its ok to not know it
bro what do you want to do exactly??
What you have to work with is this. You either bubble up the error with try, or catch to handle the error now.
In terms of syntax
check_hasError needs to unit test an error
wym unit test an error?
And errdefer is also relevant, it will run code iff error is returned
std.testing.checkHasError
Very clear
errdefer is not what I want. I can't unit test an error like that
??? them why would you even defer then
I need to unwrap the value from the error to access it
I'm going to literally block you if you don't stop calling me bruh. I'm not your bro, I'm asking a question
If you don't know the answer, thank you for the attempt at helping but you are not helping
????? What? You offended by that?
Ok, bye
look first maybe just tell me what you want first instead of being annoying
If someone knows the answer to the question, lets continue focusing on the actual topic โ๏ธ
Last time I unit tested errors, I had the expect function call inside the catch
Because catch allows you to capture the error
you could do this:
const result = function_that_returns_unionError(input, allocator) catch |e| {
if (e != ExpectedError) return error.WhateverErrorYouWant;
};
// works now, as result is no longer an error union
defer result.destroy();
Doesn't the expectHasError take a union, not a raw error?
It sure checking for the error correctly, I just can't access the value to free it
Again, that's not the usecase
:P
Mind just clarifying again the use case by editing the first message? Then by the time I reach my laptop I can take a closer look, or someone else will manage to help
The usecase is the first message running inside a unit test, really
I changed the message to use std.testing, but its literally the same thing I initially posted
uhhh, maybe you could do this:
const result = function_that_returns_unionError(input, allocator);
defer if (result) |r| r.destroy();
try std.testing.expectError(ExpectedError, result);
Yea, tried that, but it says not correct ๐คทโโ๏ธ
brush.test.zig:77:15: error: expected optional type, found '@typeInfo(@typeInfo(@TypeOf(map.brush.create.vertices)).@"fn".return_type.?).error_union.error_set!map.brush'
defer if (result) |R| R.destroy();
^~~~~~
does destroy return something that isn't void?
no
Can there be errors other than OOM?
Yes, that's the point of the unit tests
in this test case, the function is supposed to return an error
they're trying to check that the error is returned, but if it isn't, still clean up the resource
unless I'm mistaken
There will be an error for sure, the condition is erroneous on purpose. I just need to clear up the data anyway, and I found that I don't know how to unwrap the error from the value when it is stored as a union inside a variable
lemme check there might be a builtin for that
Would this work?
const result = function_that_returns_unionError(input, allocator);
if (result) |r| {
r.destroy();
try std.testing.expect(false);
} else |err| {
try std.testing.expect(err == ExpectedError);
}
๐ if (result) is invalid syntax on an error union
that's weird, I'm literally looking at the docs right now
see test "if error union"
Could this be a 0.15.1-> 0.15.2 change? ๐ค
Or maybe the docs are out of date for .15? ๐ค
I will say, that's not why you got this error
oh wait
you might need the else |err| to if on an error union
right yeah, because otherwise it would be implicitly discarding it
Nailed it! that's exactly what it was
defer if (result) |R| R.destroy() else |_| {};
TIL
glad I got there in the end :P
[solved] How do I access the data from an error!value union?
I forgot about this syntax even though I used it 2 weeks ago when testing out std.Io, because you kind of have to handle io.await and io.cancel like that