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

jinja2: removed excessive context from Jinja2 raise/assert statements #6289

Merged
merged 1 commit into from
Aug 27, 2024

Conversation

oliver-sanders
Copy link
Member

@oliver-sanders oliver-sanders commented Aug 6, 2024

User request: Make the Jinja2 raise / assert exceptions cleaner.

The raise and assert helpers are used by some workflow developers to allow validation of inputs / environment, e.g:

#!jinja2

{{ assert(SITE is defined, 'Please define the SITE variable') }}

The workflow developer configures the condition and a user-friendly message to display.

Unfortunately, that message gets lost in the resulting message:

$ cylc val .
Jinja2Error: Jinja2 Assertion Error: message
File /var/tmp/tmp.F4DJDykBo2/flow.cylc
  #!Jinja2
  
  {{ assert(SITE is defined, 'message') }}	<-- Exception

This scary looking error makes it look like something is genuinely wrong:

  • The traceback looks like program code, it's not something the user is expected to understand.
  • The Jinja2Error exception makes it look like an error.

This PR changes the exception from Jinja2Error to InputError and remove the extraneous context:

$ cylc val .
InputError: message
(add --verbose for more context)

The full exception is still available to workflows developers in verbose mode (can be helpful in tracking down the location of the assert / raise statement that caused the error).

Check List

  • I have read CONTRIBUTING.md and added my name as a Code Contributor.
  • Contains logically grouped changes (else tidy your branch by rebase).
  • Does not contain off-topic changes (use other PRs for other changes).
  • Applied any dependency changes to both setup.cfg (and conda-environment.yml if present).
  • Tests are included (or explain why tests are not needed).
  • Changelog entry included if this is a change that can affect users
  • Cylc-Doc pull request opened if required at cylc/cylc-doc/pull/XXXX.
  • If this is a bug fix, PR should be raised against the relevant ?.?.x branch.

@oliver-sanders oliver-sanders added small could be better Not exactly a bug, but not ideal. labels Aug 6, 2024
@oliver-sanders oliver-sanders added this to the 8.4.0 milestone Aug 6, 2024
@oliver-sanders oliver-sanders self-assigned this Aug 6, 2024
Copy link
Member

@hjoliver hjoliver left a comment

Choose a reason for hiding this comment

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

Looks good, couple of related but simple test failures.

@hjoliver
Copy link
Member

tests/f/jinja2/10-builtin-functions.t failing

@oliver-sanders
Copy link
Member Author

(Dammit, thought I'd removed that)

@oliver-sanders oliver-sanders requested a review from wxtim August 15, 2024 10:26
@wxtim
Copy link
Member

wxtim commented Aug 16, 2024

[Not a blocker for this PR]
This breaks Cylc Rose tests. I'll fix it and put a PR up after lunch

FAILED tests/functional/test_pre_configure.py::test_validate_fail[template variable not set] - assert []

Copy link
Member

@wxtim wxtim left a comment

Choose a reason for hiding this comment

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

This is a really, really good idea IMO.

@oliver-sanders
Copy link
Member Author

oliver-sanders commented Aug 16, 2024

This breaks Cylc Rose tests. I'll fix it and put a PR up after lunch

Of course, I completely forgot those tests were using assert! Good catch!

@oliver-sanders
Copy link
Member Author

Merging with two approvals.

@oliver-sanders oliver-sanders merged commit b0fb246 into cylc:master Aug 27, 2024
27 checks passed
@oliver-sanders oliver-sanders deleted the jinja2-assert-- branch August 27, 2024 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
could be better Not exactly a bug, but not ideal. small
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants