#how to compare error set

1 messages · Page 1 of 1 (latest)

chrome wigeon
#

how to make something like this work

if (std.fs.cwd().access("...", .{}) == std.os.AccessError) { ... }
candid lynx
#

err, well it can only return AccessError, so

std.fs.cwd.access("...", .{}) catch {
    ...
};
chrome wigeon
#

that will not work as access function itself return void

#

std.fs.cwd().access("...", .{}) catch false

candid lynx
#

What do you want to do with that false

chrome wigeon
#

I want to get true only if access return any error

candid lynx
#

But you said you wanted a block that runs on error, not a bool to store in a variable

chrome wigeon
#

ok ok, lets simplify my question
I want to check if an error is part of AccessError error set

sharp portal
#

Are you trying to just detect an error, or are you trying to detect specifically all the errors in std.os.AccessError from a function that can return more than just those errors

chrome wigeon
#

detecting any error from AccessError

#

any error at all

#

returning from access function

sharp portal
#

And what do you want to do if there is an error?

chrome wigeon
#

the if condition should be true then

sharp portal
#

So you want to do one thing if there's an error, and something else if there isn't? Or you just want to run some code when there's an error and then continue?

#

For the former, use this form: ```rs
if (std.fs.cwd().access("...", .{})) |_| {
// code to run on success
} else |err| {
// code to run on error; err contains the error value
}

For the latter, use `catch` instead of `if`: ```rs
std.fs.cwd().access("...", .{}) catch |err| {
    // code to run on error; `err` contains the error value
};
// code that runs regardless of if the function errored or not
#

If you really absolutely definitely want a boolean value (you almost certainly do not), you can use std.meta.isError

chrome wigeon
#

perfect, both last 2 solutions are exactly what I was searching for