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

miette integration for all errors in Core, Validator, and cedar-policy #477

Merged
merged 14 commits into from
Dec 8, 2023

Conversation

cdisselkoen
Copy link
Contributor

Description of changes

Derives (or manually implements) miette::Diagnostic for all our error types. Tries to make good use of the help() field, but does not yet assign error codes or propagate source locations; those can be done in followup PRs (and have their own GitHub issues).

No breaking changes to the public API. Some error messages have changed, and in particular, in some cases, information that used to be in the Display for an error is now in other miette fields. I have updated the CHANGELOG with a note about this. I'm not sure where else we should document it.

To display a miette::Diagnostic in a pretty fashion, convert it to miette::Report using From/Into. Note that ? automatically converts errors, so it is sufficient to declare the error type of your function (or main()) as miette::Report. Note also that you need the fancy feature of miette for prettiest output; we enable this in the CLI crate but not in our library crates, as recommended by miette. ("You should only [enable the "fancy" feature] in your toplevel crate, as the fancy feature pulls in a number of dependencies that libraries and such might not want.")

Sample outputs from the CLI:
image
image

Issue #, if available

Resolves #182

Checklist for requesting a review

The change in this PR is (choose one, and delete the other options):

  • A bug fix or other functionality change requiring a patch to cedar-policy.

I confirm that this PR (choose one, and delete the other options):

  • Updates the "Unreleased" section of the CHANGELOG with a description of my change (required for major/minor version bumps).

I confirm that cedar-spec (choose one, and delete the other options):

  • Does not require updates because my change does not impact the Cedar Dafny model or DRT infrastructure.

Disclaimer

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Comment on lines 66 to 67
#[error("invalid escape patterns: {:?}", .0.iter().map(|e| e.to_string()).collect::<Vec<String>>())]
UnescapeError(Vec<unescape::UnescapeError>),
UnescapeError(#[related] Vec<unescape::UnescapeError>),
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we make the error just invalid escape pattern and rely on #[related] to aggregate the errors?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could. Or we could just show the first error message in the #[error], like we do for parse errors. In #326 we decided not to totally kneecap the Display for ParseErrors in case users are only looking at the Display. We could revisit that decision, or decide differently here. Do you have a preference between no error message / first error message / all error messages?

Copy link
Contributor

Choose a reason for hiding this comment

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

It doesn't bother me too much. Using the {:?} debug formatting in error messages doesn't always work out well, but if it formats nicely in this case it's fine

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed this up some, including the error message in unescape::UnescapeError itself

UndeclaredActions(HashSet<String>),
/// Undeclared common type(s) used in entity or context attributes.
#[error("undeclared common type(s): {0:?}")]
#[diagnostic(help("any common types used in entity or context attributes need to be declared in `commonTypes`"))]
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
#[diagnostic(help("any common types used in entity or context attributes need to be declared in `commonTypes`"))]
#[diagnostic(help("any common types used in entity or context attributes need to be declared in `commonTypes`, and common types may not reference other common types"))]

We could do more to make the error in this case nice, but this at least helps

Copy link
Contributor Author

Choose a reason for hiding this comment

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

is this the error we get if we try to have common types reference other common types?

if so I can update the doc comment on the variant as well

Copy link
Contributor

Choose a reason for hiding this comment

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

That'd be good

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

cedar-policy-validator/src/type_error.rs Outdated Show resolved Hide resolved
cedar-policy-core/src/evaluator/err.rs Outdated Show resolved Hide resolved
cedar-policy/src/api.rs Outdated Show resolved Hide resolved
cedar-policy/src/api.rs Outdated Show resolved Hide resolved
@cdisselkoen cdisselkoen merged commit fe5d65c into main Dec 8, 2023
6 of 7 checks passed
@cdisselkoen cdisselkoen deleted the cdisselkoen/miette branch December 8, 2023 20:35
@cdisselkoen cdisselkoen added the 3.1 Features for 3.1 label Feb 6, 2024
@khieta khieta mentioned this pull request Jun 19, 2024
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.1 Features for 3.1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

miette integration for all errors
3 participants