Differentiate same Error from different set?

If I have two error sets containing the same named error is there any way to figure out which set the error comes from?

fn goesWrong(b: bool) !void {
    if(b) {
        return foo.somethingWentWrong;
    } else {
        return bar.somethingWentWrong;
   }
}

pub fn main() {
   goesWrong(true) catch |err| switch (err) {
       foo.somethingWentWrong => [stuff],
       bar.somethingWentWrong => [stuff]
   }
}
1 Like

Error names are global, the only way to distinguish them is to give them different names.

3 Likes

Think of it like with sets of natural numbers.

You can have A = {1, 2, 3} and B = {2, 4, 6}. These are two sets.

When you return all the errors from each set from a function, it’s like creating the union of all of them, A ∪ B = {1, 2, 3, 4, 6}.

Of course you can also not return some errors from a function, but handle them yourself. So if for example you don’t return the value 2 and handle it in your function (or ignore the error because it should never be possible from your code’s logic; e.g. the functions says 2 can only happen if you pass in x, but you never pass in x), it’s like A ∪ B \ {2} = {1, 3, 4, 6}.

3 Likes