Skip to content
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

Merged
merged 6 commits into from
Jan 8, 2021

Conversation

JeanMertz
Copy link
Contributor

@JeanMertz JeanMertz commented Jan 7, 2021

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:

// fallible, will be rejected at compile-time in the future
json = parse_json(...)

// infallible
json, err = parse_json(...)

// infallible, rejected at compile-time since the `err` case will always be null
five, err = 5

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.

@JeanMertz JeanMertz added type: feature A value-adding code addition that introduce new functionality. domain: vrl Anything related to the Vector Remap Language labels Jan 7, 2021
@JeanMertz JeanMertz added this to the 2021-01-04 Xenomass Well milestone Jan 7, 2021
@JeanMertz JeanMertz self-assigned this Jan 7, 2021
@JeanMertz
Copy link
Contributor Author

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 err being set to null.

Signed-off-by: Jean Mertz <git@jeanmertz.com>
Signed-off-by: Jean Mertz <git@jeanmertz.com>
@JeanMertz JeanMertz force-pushed the jean/remap-error-return-value branch from d291353 to 484be33 Compare January 8, 2021 09:21
@JeanMertz
Copy link
Contributor Author

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"#),
Copy link
Contributor Author

@JeanMertz JeanMertz Jan 8, 2021

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
Copy link
Contributor Author

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>
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> },
}
Copy link
Contributor

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.

Copy link
Contributor Author

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)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, tracking in #5940.

Copy link
Contributor

@StephenWakely StephenWakely left a 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.

@StephenWakely StephenWakely self-requested a review January 8, 2021 12:33
@JeanMertz
Copy link
Contributor Author

Moving this into master, but still happy to get your feedback @jszwedko, I'll process them in a follow-up PR if needed.

@JeanMertz JeanMertz merged commit 577edaf into master Jan 8, 2021
@JeanMertz JeanMertz deleted the jean/remap-error-return-value branch January 8, 2021 12:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain: vrl Anything related to the Vector Remap Language type: feature A value-adding code addition that introduce new functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remap error return value
2 participants