# Custom filter plugin - how to throw an error

**URL:** <https://forum.ansible.com/t/custom-filter-plugin-how-to-throw-an-error/3911>\
**Category:** Get Help\
**Tags:** playbook\
**Created:** [February 16, 2024, 9:42pm UTC](https://forum.ansible.com/t/custom-filter-plugin-how-to-throw-an-error/3911 "2024-02-16T21:42:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jms1](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jms1/32/2255_2.png) [@jms1](https://forum.ansible.com/u/jms1)\
**Post date:** [February 16, 2024, 9:42pm UTC](https://forum.ansible.com/t/custom-filter-plugin-how-to-throw-an-error/3911/1 "2024-02-16T21:42:07Z")

</div>

I’m trying to write a custom filter plugin, and I need it to throw an error. What I have so far …

- `filter_plugins/custom.py`

- `doit.yml` (playbook)

- Output

I suspect I need to `import` something at the top of the Python file, but … I’ll be honest, today was the first day I’ve ever tried to write _anything_ in Python, and all of the documentation I’ve found about this, seems to assume that the reader is already familiar with the language, and with enough of Ansible’s internals that they already _know_ whatever it is I’m missing here.

---

<div class="post-metadata">

**Author:** ![spike](https://avatars.discourse-cdn.com/v4/letter/s/8797f3/32.png) [@spike](https://forum.ansible.com/u/spike)\
**Post date:** [February 16, 2024, 9:56pm UTC](https://forum.ansible.com/t/custom-filter-plugin-how-to-throw-an-error/3911/2 "2024-02-16T21:56:31Z")

</div>

You can find all ansible specific error classes at [ansible/lib/ansible/errors/\_\_init\_\_.py at devel · ansible/ansible · GitHub](https://github.com/ansible/ansible/blob/devel/lib/ansible/errors/ __init__.py)

You probably want to use `AnsibleFilterError` since it’s a custom filter plugin:

```auto
from ansible.errors import AnsibleFilterError
...
raise AnsibleFilterError(...)

```

Hope that helps!

---

<div class="post-metadata">

**Author:** ![jms1](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jms1/32/2255_2.png) [@jms1](https://forum.ansible.com/u/jms1)\
**Post date:** [February 16, 2024, 10:02pm UTC](https://forum.ansible.com/t/custom-filter-plugin-how-to-throw-an-error/3911/3 "2024-02-16T22:02:09Z")

</div>

Updated the plugin …

```python
#!/usr/bin/python

from ansible.errors import AnsibleFilterError

class FilterModule( object ) :
    def filters( self ) :
        return { 'custom' : self._custom }

    def _custom( self , input : str ) :
        if not input.startswith( '/' ) :
            raise AnsibleFilterError( "input must start with '/'" )

```

And now the output is …

```auto
TASK [fail] ********************************************************
fatal: [localhost]: FAILED! => {"msg": "input must start with '/'"}

```

Thank you VERY much!

Hopefully next week I’ll have some time to start getting familiar with Python itself, so I’m better able to _read_ what I’m finding on Github, and so I know what an ` __init__.py` file is for in the first place.

Have a good weekend!

---

<div class="post-metadata">

**Author:** ![spike](https://avatars.discourse-cdn.com/v4/letter/s/8797f3/32.png) [@spike](https://forum.ansible.com/u/spike)\
**Post date:** [February 16, 2024, 10:07pm UTC](https://forum.ansible.com/t/custom-filter-plugin-how-to-throw-an-error/3911/4 "2024-02-16T22:07:23Z")

</div>

No worries. Happy to help!
