Skip to content

bug: Improve exception handling when user uses an invalid jinja template #8311

Description

@minitriga

Component

No response

Infrahub version

1.7.4

Current Behavior

In any schema when an invalid template like display_label: "{{ }} testing" is used then loading the schema fails:

❯ uv run infrahubctl schema load schemas
HTTP communication failure: Server error '500 Internal Server Error' for url 'http://localhost:8000/api/schema/load?branch=main'
For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500 on POST to http://localhost:8000/api/schema/load?branch=main

Exception:

  File "/source/backend/infrahub/core/schema/schema_branch.py", line 1303, in _validate_display_label

    variables = jinja_template.get_variables()

  File "/source/python_sdk/infrahub_sdk/template/__init__.py", line 85, in get_variables

    template = env.parse(template_source)

  File "/.venv/lib/python3.13/site-packages/jinja2/environment.py", line 616, in parse

    self.handle_exception(source=source)

    ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^

  File "/.venv/lib/python3.13/site-packages/jinja2/environment.py", line 942, in handle_exception

    raise rewrite_traceback_stack(source=source)

  File "<unknown>", line 1, in template

jinja2.exceptions.TemplateSyntaxError: Expected an expression, got 'end of print statement'

Expected Behavior

Gracefully handle and inform user about the exception.

Steps to Reproduce

Load this schema into a blank infrahub instance:

---
# yaml-language-server: $schema=https://schema.infrahub.app/infrahub/schema/latest.json
version: "1.0"

nodes:
  # --------------------  IPAM (Prefix, IP Address)  --------------------
  - name: IPAddress
    namespace: Ipam
    description: IP Address
    label: IP Address
    icon: mdi:ip
    include_in_menu: false
    order_by:
      - address__value
    display_label: "{{ }} asdas"
    inherit_from:
      - BuiltinIPAddress
    uniqueness_constraints:
      - [address__value, ip_namespace]
    human_friendly_id:
      - address__value
      - ip_namespace__name__value
    attributes:
      - name: fqdn
        label: FQDN
        kind: Text
        optional: true
        # https://stackoverflow.com/questions/11809631/fully-qualified-domain-name-validation
        regex: (?=^.{1,253}$)(^(((?!-)[a-zA-Z0-9-]{1,63}(?<!-))|((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+[a-zA-Z]{2,63})$)
    relationships:
      - name: interface
        peer: InterfaceLayer3
        optional: true
        cardinality: one

Additional Information

No response

Metadata

Metadata

Assignees

Labels

type/bugSomething isn't working as expected

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions