-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
feat(remap): error-return-value error handling pattern #5911
Conversation
Ah wait, I just realised I forgot to add one more feature to this PR. I want to make this pattern result in a compile-time error: five, err = 5 That is to say, if a rhs expression is known to be infallible, we want to reject error assignments, since it only confuses readers and will only ever result in |
Signed-off-by: Jean Mertz <git@jeanmertz.com>
Signed-off-by: Jean Mertz <git@jeanmertz.com>
d291353
to
484be33
Compare
Alright, this one is good to review at this point. cc @jszwedko @FungusHumungus. |
Signed-off-by: Jean Mertz <git@jeanmertz.com>
@@ -294,6 +294,46 @@ mod tests { | |||
Err(r#"remap error: error for function "map_printer": cannot mark infallible function as "abort on error", remove the "!" signature"#), | |||
Ok(().into()), | |||
), | |||
( | |||
"$foo, $err = fallible_func!()", | |||
Err(r#"remap error: assignment error: the variable "foo" does not need to handle the error-case, because its result is infallible"#), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left a comment in #4803 (comment) to further improve this error message sometime next week when we're going to tackle compiler error messages.
Signed-off-by: Jean Mertz <git@jeanmertz.com>
@@ -89,8 +89,9 @@ impl Program { | |||
source: &str, | |||
function_definitions: &[Box<dyn Function>], | |||
constraint: Option<TypeConstraint>, | |||
allow_regex_return: bool, // TODO: move this into a builder pattern |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added this to make it easier to write some of our tests, that don't care about the return value.
Eventually, if/when VRL is its own project (it is, but it's still in the Vector repo right now), we'll change this so that Vector either handle regex return values, or we raise an error in Vector itself instead of here.
But the TL;DR is: please ignore this for now 😄
Signed-off-by: Jean Mertz <git@jeanmertz.com>
} | ||
|
||
#[derive(Debug, Clone, PartialEq)] | ||
pub enum Target { | ||
Path(Path), | ||
Variable(Variable), | ||
Infallible { ok: Box<Target>, err: Box<Target> }, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could take fuller advantage of Rusts type system here to make illegal states unrepresentable.
pub enum Target {
Path(Path),
Variable(Variable)
}
pub enum AssignmentTarget {
Fallible { ok: Box<Target> },
Infallible { ok: Box<Target>, err: Box<Target> }
}
This makes it logically impossible to specify nested Infallible targets. It is much clearer what this code is expecting. Any bugs there will just be caught quickly by the compiler and we won't need all the unimplemented!
calls below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about doing that, but I didn't see the value in this specific case (I'm not averse to using unimplemented
, since it's covered by the initializer and the parser, so if you ever change this, many tests would fail).
Would you be okay with me filling this as an issue for a future improvement, so that I can keep the pace up for 0.12 (this also requires some re-jiggling in the parser and module exporting, etc)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, tracking in #5940.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be much clearer to reorganise those Structs a bit.
Moving this into master, but still happy to get your feedback @jszwedko, I'll process them in a follow-up PR if needed. |
Another step towards solving our error-handling story as described in #5479 and this gist.
You can now make the rhs expression in an assignment infallible by capturing the error message:
err
is null if the rhs expression succeeded, otherwise it contains the error message as a string.This joins the existing error-handling strategies introduced in #5876 and #5830.
note: this does not yet implement the "ignore-target" (e.g.
json, _ = parse_json(...)
), I'll create a follow-up issue to add that, as it's not strictly needed for this to be useful.closes #5865.