#error handling this tedious?

1 messages · Page 1 of 1 (latest)

honest drift
#

hey so when we are trying to catch an error that is already defined in the std under a specific type does the ZLS really not have that information to autocomplete or am i doing something wrong that it cant reach it lol:
https://ziglang.org/documentation/master/std/#std.process.changeCurDir

pub fn cd(req_dir: []const u8) !void {
    try std.process.changeCurDir(req_dir) catch |err| switch (err) {
        error.NotDir => {
            std.debug.print("The Path is Not a Dir: {s}\n", .{req_dir});
            return err;
        },
        error.FileNotFound => {
            std.debug.print("Directory not found: {s}\n", .{req_dir});
            return err;
        },
        error.AccessDenied => {
            std.debug.print("Access denied to directory: {s}\n", .{req_dir});
            return err;
        },
        error.NameTooLong => {
            std.debug.print("Path name is too long: {s}\n", .{req_dir});
            return err;
        },
        else => {
            std.debug.print("There is another problem with {s}\n", .{req_dir});
            return err;
        },
    };
}
opal sphinx
#

You don't want both try and catch, because try is just EXPR catch |err| return err.

honest drift
#

yeah i see that now i changed that.

#

i missed that. thank you.

opal sphinx
#

I might consider factoring the message out, but otherwise that seems largely fine.
You want a different message per error code, so you have a different message based on the error code.

std.process.changeCurDir(req_dir) catch |err| {
    const msg = switch (err) {
        error.NotDir       => "The path is not a directory",
        error.FileNotFound => "Directory not found",
        error.AccessDenied => "Access denied",
        error.NameTooLong  => "Path name is too long",
        else               => "Unknown error occurred",
    };
    std.debug.print("error: {s}: {s}\n", .{ msg, req_dir });
    return err;
}
warm aspen
#

that doesn't include the request directory in the error message

opal sphinx
#

Yes it does 😉

honest drift
#

very nice. nut i guess i would like to know how come the zls cant find the error fields it self? lol. i have to look up each type and see the error fields thaty have 😦