# Controlling failure on checking jinja2 variable

**URL:** <https://cylc.discourse.group/t/controlling-failure-on-checking-jinja2-variable/238>\
**Category:** Cylc Support\
**Created:** [May 6, 2020, 4:58pm UTC](https://cylc.discourse.group/t/controlling-failure-on-checking-jinja2-variable/238 "2020-05-06T16:58:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikethurlow](https://yyz2.discourse-cdn.com/free1/user_avatar/cylc.discourse.group/mikethurlow/32/131_2.png) [@mikethurlow](https://cylc.discourse.group/u/mikethurlow)\
**Post date:** [May 6, 2020, 4:58pm UTC](https://cylc.discourse.group/t/controlling-failure-on-checking-jinja2-variable/238/1 "2020-05-06T16:58:21Z")

</div>

Hi

One of my suites has a jinja2 section at the start of the suite.rc file which does a lot of variable setting based on other jinja2 variables set elsewhere (in a rose-suite.conf). A simplified version of part of it would look something like

{% if SUITE\_RESOLUTION == “low” %}  
{% set GL\_UM\_RES = “n320e” %}  
…  
{% elif SUITE\_RESOLUTION == “medium” %}  
{% set GL\_UM\_RES = “n640e” %}  
…  
{% elif SUITE\_RESOLUTION == “high” %}  
{% set GL\_UM\_RES = “n1280e” %}  
…  
{% else %}  
…  
{% endif %}

If I happen to set a non-valid value of SUITE-RESOLUTION then cylc validate will crash out as expected but with a non-helpful (for the general user) error message. Is there a way of getting cylc validate to raise an error with a meaningful message? I doubt there is but I thought I would ask of the wise.  
If not does anyone have any other suggestions as to how to make the desired failure less of a mess? Something in the {% else %} section perhaps?

Thanks

Mike

---

<div class="post-metadata">

**Author:** ![hilary.j.oliver](https://yyz2.discourse-cdn.com/free1/user_avatar/cylc.discourse.group/hilary.j.oliver/32/4_2.png) [@hilary.j.oliver](https://cylc.discourse.group/u/hilary.j.oliver)\
**Post date:** [May 6, 2020, 10:14pm UTC](https://cylc.discourse.group/t/controlling-failure-on-checking-jinja2-variable/238/2 "2020-05-06T22:14:58Z")

</div>

Hi @mikethurlow,

I’m not sure I see the problem. From what you’ve written, there isn’t any “non-valid value of SUITE\_RESOLUTION” because you have an `{% else %}` block to catch everything other than “low”, “medium”, and “high”. The only way I can trigger a Jinja2 error is to not set SUITE\_RESOLUTION at all, which results in a pretty clear message:

```auto
Jinja2Error:
  File "<template>", line 2, in top-level template code
UndefinedError: 'SUITE_RESOLUTION' is undefined
Context lines:
{% if SUITE_RESOLUTION == "low" %}	<-- Jinja2Error

```

Do you mean the else block should abort with an error message because only low, medium, and high are valid?

```auto
{% else %}
  {{ raise("invalid value of SUITE_RESOLUTION: " + SUITE_RESOLUTION) }}
{% endif %}

```

Then:

```auto
$ cylc validate --set="SUITE_RESOLUTION=huge" mt
Jinja2 Error: invalid value of SUITE_RESOLUTION: huge

```

Hilary

p.s. documentation of `raise()` in the current online user guide: [https://cylc.github.io/doc/built-sphinx-single/index.html#raising-exceptions](https://cylc.github.io/doc/built-sphinx-single/index.html#raising-exceptions)

---

<div class="post-metadata">

**Author:** ![mikethurlow](https://yyz2.discourse-cdn.com/free1/user_avatar/cylc.discourse.group/mikethurlow/32/131_2.png) [@mikethurlow](https://cylc.discourse.group/u/mikethurlow)\
**Post date:** [May 7, 2020, 8:32am UTC](https://cylc.discourse.group/t/controlling-failure-on-checking-jinja2-variable/238/3 "2020-05-07T08:32:05Z")

</div>

> [@hilary.j.oliver](#):
>
> raise

Thanks Hilary. I didn’t know about “raise” and that solves the problem!  
Mike

---

<div class="post-metadata">

**Author:** ![TomC](https://yyz2.discourse-cdn.com/free1/user_avatar/cylc.discourse.group/tomc/32/115_2.png) [@TomC](https://cylc.discourse.group/u/TomC)\
**Post date:** [May 11, 2020, 8:40pm UTC](https://cylc.discourse.group/t/controlling-failure-on-checking-jinja2-variable/238/4 "2020-05-11T20:40:27Z")

</div>

There is also the assert function in jinja2, documented at [https://cylc.github.io/doc/built-sphinx-single/index.html#assert](https://cylc.github.io/doc/built-sphinx-single/index.html#assert) which can be very useful.

---

<div class="post-metadata">

**Author:** ![mikethurlow](https://yyz2.discourse-cdn.com/free1/user_avatar/cylc.discourse.group/mikethurlow/32/131_2.png) [@mikethurlow](https://cylc.discourse.group/u/mikethurlow)\
**Post date:** [May 12, 2020, 8:31am UTC](https://cylc.discourse.group/t/controlling-failure-on-checking-jinja2-variable/238/5 "2020-05-12T08:31:05Z")

</div>

Thanks Tom. I should really start thinking about jinja2 and python more holistically than I have been.

Mike

---

<div class="post-metadata">

**Author:** ![fredw](https://yyz2.discourse-cdn.com/free1/user_avatar/cylc.discourse.group/fredw/32/112_2.png) [@fredw](https://cylc.discourse.group/u/fredw)\
**Post date:** [May 12, 2020, 9:00am UTC](https://cylc.discourse.group/t/controlling-failure-on-checking-jinja2-variable/238/6 "2020-05-12T09:00:50Z")

</div>

Hello Mike,

I have a similar thing in one of my rose/cylc suites. In my case I am deciding which .rc file (containing site-specific settings) to include, based on a SITE variable in the rose-suite.conf file:

```
{% if SITE == 'met' %}
    %include 'site_rc/suite-runtime-met.rc'
{% elif SITE == 'uni' %}
    %include 'site_rc/suite-runtime-uni.rc'
{% elif SITE == 'ofc' %}
    %include 'site_rc/suite-runtime-ofc.rc'
{% else %}
    {{ "ERROR: unknown SITE value!"/0 }}
{% endif %}

```

When validating with an invalid SITE value you’ll see that as an ERROR in the shell.  
Not very elegant, but it works as a reminder.

---

<div class="post-metadata">

**Author:** ![hilary.j.oliver](https://yyz2.discourse-cdn.com/free1/user_avatar/cylc.discourse.group/hilary.j.oliver/32/4_2.png) [@hilary.j.oliver](https://cylc.discourse.group/u/hilary.j.oliver)\
**Post date:** [May 13, 2020, 10:56pm UTC](https://cylc.discourse.group/t/controlling-failure-on-checking-jinja2-variable/238/7 "2020-05-13T22:56:20Z")

</div>

Hi @fredw,

Yeah that’s another approach. It works because your Jinja2 print statement `{{ "ERROR ...." }}` prints a string (`"ERROR ..."`) that is not valid a Cylc configuration item, so the config parser will abort and print the invalid line.

Hilary

---

<div class="post-metadata">

**Author:** ![mikethurlow](https://yyz2.discourse-cdn.com/free1/user_avatar/cylc.discourse.group/mikethurlow/32/131_2.png) [@mikethurlow](https://cylc.discourse.group/u/mikethurlow)\
**Post date:** [May 14, 2020, 7:49am UTC](https://cylc.discourse.group/t/controlling-failure-on-checking-jinja2-variable/238/8 "2020-05-14T07:49:44Z")

</div>

Yes that approach was similar to what I was trying previously. Having a meaningful error message is important for users who don’t like/want to unpick the automatic and cryptic that cylc validate can produce in such circumstances.
