#[solved] How do I access the data from an error!value union?

1 messages ยท Page 1 of 1 (latest)

upper cave
#
const result = function_that_returns_unionError(input, allocator);
defer result.destroy();  // <--------- cant do that directly
try std.testing.expectError(ExpectedError, result);
```How is this done? ๐Ÿค”
woven otter
#

use catch

upper cave
#

what's the syntax for it?

earnest thunder
#

Typing on my phone:

const result_maybe = ....
const result = result_maybe catch {...};
defer result.destroy(); // or errdefer if lifetime is longer than this scope
woven otter
#

function_that_returnerror() catch |err| { ... do what u want with err here }

upper cave
#

I can't do that. You missed the check_hasError part

woven otter
#

just put if inside catch bruh

upper cave
#

I need to unit test it first, then clean it

upper cave
#

I'm asking for syntax

#

If you don't know it, its ok to not know it

woven otter
#

bro what do you want to do exactly??

earnest thunder
#

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

upper cave
#

check_hasError needs to unit test an error

woven otter
#

wym unit test an error?

earnest thunder
#

And errdefer is also relevant, it will run code iff error is returned

upper cave
#

std.testing.checkHasError

#

Very clear

#

errdefer is not what I want. I can't unit test an error like that

woven otter
#

??? them why would you even defer then

upper cave
#

I need to unwrap the value from the error to access it

woven otter
#

use catch bruh

#

thats what catch does

upper cave
#

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

woven otter
#

????? What? You offended by that?

upper cave
#

Ok, bye

woven otter
#

look first maybe just tell me what you want first instead of being annoying

upper cave
#

If someone knows the answer to the question, lets continue focusing on the actual topic โœ๏ธ

earnest thunder
#

Last time I unit tested errors, I had the expect function call inside the catch

#

Because catch allows you to capture the error

toxic mulch
upper cave
#

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

toxic mulch
#

oh wait

#

I'm wrong

toxic mulch
#

:P

earnest thunder
#

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

upper cave
#

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

toxic mulch
#

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);
upper cave
#

Yea, tried that, but it says not correct ๐Ÿคทโ€โ™‚๏ธ

upper cave
toxic mulch
#

does destroy return something that isn't void?

upper cave
#

no

earnest thunder
#

Can there be errors other than OOM?

upper cave
#

Yes, that's the point of the unit tests

toxic mulch
#

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

upper cave
#

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

toxic mulch
#

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);
}
upper cave
toxic mulch
#

that's weird, I'm literally looking at the docs right now

#

see test "if error union"

upper cave
#

Could this be a 0.15.1-> 0.15.2 change? ๐Ÿค”

#

Or maybe the docs are out of date for .15? ๐Ÿค”

toxic mulch
#

seems unlikely

#

I'll swap back to 0.15.1

toxic mulch
#

oh wait

#

you might need the else |err| to if on an error union

#

right yeah, because otherwise it would be implicitly discarding it

upper cave
#

Nailed it! that's exactly what it was

defer if (result) |R| R.destroy() else |_| {};
toxic mulch
#

TIL

upper cave
#

yea same

#

great to know!

#

ty for the advice ๐Ÿฅณ ๐Ÿ™‚

toxic mulch
#

glad I got there in the end :P

upper cave
#

[solved] How do I access the data from an error!value union?

earnest thunder
#

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