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

Correct the error message shown for invalid regex errors #76

Closed
wants to merge 1 commit into from

Conversation

edmorley
Copy link
Member

@edmorley edmorley commented Jun 20, 2022

There is currently only one error variant for ProcfileParsingError, an error that only occurs if it was not possible to compile the regex used to parse a Procfile.

This regex is buildpack-supplied, so if such an error occurs, it's always an internal buildpack error.

However previously a user-facing error message was shown, that told the user to check their Procfile was valid, which has no bearing on whether the regex will compile.

I believe part of the reason for this confusion over error messages is that the error message handling was split across multiple files due to the use of thiserror meaning that the display representation could be leant upon, without actually looking closely at what was being displayed.

As such, I've removed thiserror, since I think the error handling is easier to reason about without it.

Fixes #74.
GUS-W-11318868.

@edmorley edmorley self-assigned this Jun 20, 2022
@edmorley edmorley force-pushed the edmorley/correct-parsing-error-message branch from d70d9e4 to a3b5bf3 Compare June 20, 2022 13:51
@edmorley edmorley marked this pull request as ready for review June 20, 2022 13:57
@edmorley edmorley requested a review from a team as a code owner June 20, 2022 13:57
There is currently only one error variant for `ProcfileParsingError`,
an error that only occurs if it was not possible to compile the regex
used to parse a `Procfile`.

This regex is buildpack-supplied, so if such an error occurs, it's always
an internal buildpack error.

However previously a user-facing error message was shown, that told
the user to check their `Procfile` was valid, which has no bearing on
whether the regex will compile.

I believe part of the reason for this confusion over error messages is
that the error message handling was split across multiple files due to
the use of `thiserror` meaning that the display representation could
be leant upon, without actually looking closely at what was being
displayed.

As such, I've removed `thiserror`, since I think the error handling is
easier to reason about without it.

Fixes #74.
GUS-W-11318868.
@edmorley edmorley force-pushed the edmorley/correct-parsing-error-message branch from a3b5bf3 to 3745d65 Compare June 20, 2022 14:08
ProcfileParsingError(#[from] ProcfileParsingError),
#[error("Procfile conversion error: {0}")]
ProcfileConversionError(#[from] ProcfileConversionError),
Parsing(ProcfileParsingError),
Copy link
Member

Choose a reason for hiding this comment

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

FWIW, nit: I like having the name of the enum variant being the same name as the error it wraps if there is no additional context to be added.

Copy link
Member Author

Choose a reason for hiding this comment

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

So for my Python CNB I've tried to avoid redundancy in the enum variant names where possible. The enum type itself is named ProcfileBuildpackError, so by definition we know it's Procfile related, and also they are all errors. Having shorter names then makes the nested matches slightly more readable (all of the variables starting with Procfile... were blurring into one).

However I can also see that having the names match may be appealing too.

Comment on lines +37 to +43
Regex::new("\\r\\n?").map_err(ProcfileParsingError::InvalidRegex)?;
let re_multiple_newline =
Regex::new("\\n*\\z").map_err(ProcfileParsingError::RegexError)?;
Regex::new("\\n*\\z").map_err(ProcfileParsingError::InvalidRegex)?;

// https://github.com/heroku/codon/blob/2613554383cb298076b4a722f4a1aa982ad757e6/lib/slug_compiler/slug.rb#L538-L545
let re_procfile_entry = Regex::new("^[[:space:]]*([a-zA-Z0-9_-]+):?\\s+(.*)[[:space:]]*")
.map_err(ProcfileParsingError::RegexError)?;
.map_err(ProcfileParsingError::InvalidRegex)?;
Copy link
Member

Choose a reason for hiding this comment

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

I would even go so far and say these arent ProcfileParsingErrors. When they occur, it's clearly a programmer error on procfile-cnbs part. Those don't have to be errors at runtime IMO. This is one of the rare occurrences where I think panicing is fine.

I suggest we remove the InvalidRegex variant and call expect instead of mapping. Clippy will actually verify that the regex in our source is valid/compilable - so actual panics should never happen if our CI setup is correct. https://rust-lang.github.io/rust-clippy/master/#invalid_regex

This would make things much easier and would've prevented the incorrect error message in the first place.

Copy link
Member Author

@edmorley edmorley Jun 20, 2022

Choose a reason for hiding this comment

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

I also found https://github.com/Canop/lazy-regex which does compile-time regex validation - however I'm slightly reluctant to switch to a less popular crate that adds another layer of abstraction around regex.

Agree about Clippy validation, and I'd be fine with .expect() with a comment too.

Copy link
Member

Choose a reason for hiding this comment

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

I'll mark my review as "changes required" then :)

Copy link
Member Author

@edmorley edmorley Jun 20, 2022

Choose a reason for hiding this comment

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

So once those errors are removed, impl FromStr for Procfile never returns an error.
As such, I was thinking I could just use type Err = Infallible (std::convert::Infallible).

However it seems conversion from infallible (without using eg unwrap()) is still pretty rough around the edges, eg this unstabilised feature:
rust-lang/rust#61695

One option would be to drop the FromStr and just use the infallible impl From<&str> for Procfile instead? The API would at least more closely match reality then. However if we ever make things more strict #73 then that would change again (though given private APIs doesn't really matter I suppose).

Thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

From<&str> sounds good to me - if it's really infallible there is no need to encode errors.

Copy link
Member Author

@edmorley edmorley Jun 20, 2022

Choose a reason for hiding this comment

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

So thinking about this some more, rather than switch from FromStr to impl From<&str> for Procfile now, only to then have to switch to either TryFrom or back to FromStr in the future (when we fix #73), I'm leaning towards using an empty error variant (pub enum ProcfileParsingError {}) instead (into which we can then add the future errors).

Copy link
Member Author

Choose a reason for hiding this comment

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

From an API standpoint I think that models the situation reasonably well: "a function that should be treated as fallible, but right now there are zero implemented modes of failure".

Copy link
Member

Choose a reason for hiding this comment

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

Works for me too. Whatever models reality the best - if we expect there to be errors soon we can keep TryFrom. :)

Copy link
Member

@Malax Malax left a comment

Choose a reason for hiding this comment

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

See earlier comments

@edmorley
Copy link
Member Author

Superseded by #77.

@edmorley edmorley closed this Jun 20, 2022
@edmorley edmorley deleted the edmorley/correct-parsing-error-message branch June 20, 2022 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error message shown for ProcfileParsingError is not accurate
2 participants