Improve Error Handling in Built-In Webhook Handler (#3987)

This commit is contained in:
Poolitzer 2023-12-09 17:35:23 +01:00 committed by GitHub
parent 592c6cc6d3
commit 67b0706116
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 5 deletions

View file

@ -146,6 +146,9 @@ class TelegramHandler(tornado.web.RequestHandler):
"Received data was *not* processed!",
exc_info=exc,
)
raise tornado.web.HTTPError(
HTTPStatus.BAD_REQUEST, reason="Update could not be processed"
) from exc
if update:
_LOGGER.debug(

View file

@ -63,6 +63,7 @@ class TestUpdater:
cb_handler_called = None
offset = 0
test_flag = False
response_text = "<html><title>{1}: {0}</title><body>{1}: {0}</body></html>"
@pytest.fixture(autouse=True)
def _reset(self):
@ -732,11 +733,10 @@ class TestUpdater:
if secret_token:
# Returns Forbidden if no secret token is set
response_text = "<html><title>403: {0}</title><body>403: {0}</body></html>"
response = await send_webhook_message(ip, port, update.to_json(), "TOKEN")
assert response.status_code == HTTPStatus.FORBIDDEN
assert response.text == response_text.format(
"Request did not include the secret token"
assert response.text == self.response_text.format(
"Request did not include the secret token", HTTPStatus.FORBIDDEN
)
# Returns Forbidden if the secret token is wrong
@ -744,7 +744,9 @@ class TestUpdater:
ip, port, update.to_json(), "TOKEN", secret_token="NotTheSecretToken"
)
assert response.status_code == HTTPStatus.FORBIDDEN
assert response.text == response_text.format("Request had the wrong secret token")
assert response.text == self.response_text.format(
"Request had the wrong secret token", HTTPStatus.FORBIDDEN
)
await updater.stop()
assert not updater.running
@ -1071,11 +1073,15 @@ class TestUpdater:
# Now, we send an update to the server
update = make_message_update("Webhook")
with caplog.at_level(logging.CRITICAL):
await send_webhook_message(ip, port, update.to_json(), "TOKEN")
response = await send_webhook_message(ip, port, update.to_json(), "TOKEN")
assert len(caplog.records) == 1
assert caplog.records[-1].getMessage().startswith("Something went wrong processing")
assert caplog.records[-1].name == "telegram.ext.Updater"
assert response.status_code == 400
assert response.text == self.response_text.format(
"Update could not be processed", HTTPStatus.BAD_REQUEST
)
# Make sure that everything works fine again when receiving proper updates
caplog.clear()