Use voluptuous error string for websocket validation error (#21883)

* use voluptuous error string to websocket validation error

* added exception logging to websocket error

* add detailed message to websocket validation error

* add error message to websocket validation error

* Add humanize error for websocket invalid vol error

* Add humanize error for websocket invalid vol error

* Add humanize error for websocket invalid vol error
This commit is contained in:
Leonardo Merza 2019-03-27 13:40:39 -04:00 committed by Paulus Schoutsen
parent 52437f6246
commit 29ad3961e5
2 changed files with 26 additions and 2 deletions

View file

@ -93,11 +93,11 @@ class ActiveConnection:
err_message = 'Unauthorized'
elif isinstance(err, vol.Invalid):
code = const.ERR_INVALID_FORMAT
err_message = 'Invalid format'
err_message = vol.humanize.humanize_error(msg, err)
else:
self.logger.exception('Error handling message: %s', msg)
code = const.ERR_UNKNOWN_ERROR
err_message = 'Unknown error'
self.logger.exception('Error handling message: %s', err_message)
self.send_message(
messages.error_message(msg['id'], code, err_message))

View file

@ -4,6 +4,7 @@ from unittest.mock import patch, Mock
from aiohttp import WSMsgType
import pytest
import voluptuous as vol
from homeassistant.components.websocket_api import const, messages
@ -90,3 +91,26 @@ async def test_handler_failing(hass, websocket_client):
assert msg['type'] == const.TYPE_RESULT
assert not msg['success']
assert msg['error']['code'] == const.ERR_UNKNOWN_ERROR
async def test_invalid_vol(hass, websocket_client):
"""Test a command that raises invalid vol error."""
hass.components.websocket_api.async_register_command(
'bla', Mock(side_effect=TypeError),
messages.BASE_COMMAND_MESSAGE_SCHEMA.extend({
'type': 'bla',
vol.Required('test_config'): str
}))
await websocket_client.send_json({
'id': 5,
'type': 'bla',
'test_config': 5
})
msg = await websocket_client.receive_json()
assert msg['id'] == 5
assert msg['type'] == const.TYPE_RESULT
assert not msg['success']
assert msg['error']['code'] == const.ERR_INVALID_FORMAT
assert 'expected str for dictionary value' in msg['error']['message']