ansible/event-driven-ansible

webhook source plugins returns an uncontrolled 500 exception when the request doesn't contain a valid JSON body

Alex-Izquierdo opened this issue · 1 comments

How to reproduce:

  • Run a basic rulebook with the webhook source plugin.
  • Run a POST request with a body that is not a valid JSON string.

Actual result:

curl http://localhost:5001/ -d 'not a json'

response:

500 Internal Server Error

Rulebook output:

2023-07-31 13:38:59,962 - aiohttp.server - ERROR - Error handling request
Traceback (most recent call last):
  File "/home/alex/repos/redhat/ansible/eda/ansible-rulebook/venv/lib64/python3.9/site-packages/aiohttp/web_protocol.py", line 433, in _handle_request
    resp = await request_handler(request)
  File "/home/alex/repos/redhat/ansible/eda/ansible-rulebook/venv/lib64/python3.9/site-packages/aiohttp/web_app.py", line 504, in _handle
    resp = await handler(request)
  File "/home/alex/.ansible/collections/ansible_collections/ansible/eda/extensions/eda/plugins/event_source/webhook.py", line 30, in webhook
    payload = await request.json()
  File "/home/alex/repos/redhat/ansible/eda/ansible-rulebook/venv/lib64/python3.9/site-packages/aiohttp/web_request.py", line 671, in json
    return loads(body)
  File "/usr/lib64/python3.9/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/usr/lib64/python3.9/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib64/python3.9/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Expected result:

Webhook plugin should handle this situation either returning an 400 with an exception indicating that only json is supported or to return an 200 accepting a non-json body.