-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Is it possible to embed information in errors? #572
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The suggestion for this situation is to make your own type with const Result = enum {
Ok,
InvalidChar: usize,
};
fn parse(input: []const u8) -> Result {
// if it works
return Result.Ok{};
// if it has unexpected char
return Result.InvalidChar { position };
} |
This is brushing against a general issue which is that zig error codes are used a lot like exception objects (in Java, for example), but zig error codes are missing lots of useful features of exception objects. Another missing feature that would be useful is a stack trace. |
Thanks @andrewrk, I think that makes sense to me. Regarding stack traces, I think it would be useful if there was a compiler function to get current line number, e.g. Though I'm not sure how that would interact with the |
I agree, I was thinking along the same lines. Want to make a new issue with a proposal? We can use that to track the feature. |
opened #577 |
The proposed solution will not work with errdefer though? |
In this case you can populate the error data, and then return a general error such as |
For example, say I'm parsing something and I hit an unexpected character, so I do something like:
But I also want the error to carry information about which char was invalid.
Is there a way to do that?
The text was updated successfully, but these errors were encountered: