diff --git a/homeassistant/components/cloud/http_api.py b/homeassistant/components/cloud/http_api.py index 03a77c08d4b..a2825eb6d7b 100644 --- a/homeassistant/components/cloud/http_api.py +++ b/homeassistant/components/cloud/http_api.py @@ -107,13 +107,16 @@ def _handle_cloud_errors(handler): result = await handler(view, request, *args, **kwargs) return result - except (auth_api.CloudError, asyncio.TimeoutError) as err: + except Exception as err: # pylint: disable=broad-except err_info = _CLOUD_ERRORS.get(err.__class__) if err_info is None: + _LOGGER.exception( + "Unexpected error processing request for %s", request.path) err_info = (502, 'Unexpected error: {}'.format(err)) status, msg = err_info - return view.json_message(msg, status_code=status, - message_code=err.__class__.__name__) + return view.json_message( + msg, status_code=status, + message_code=err.__class__.__name__.lower()) return error_handler diff --git a/tests/components/cloud/test_http_api.py b/tests/components/cloud/test_http_api.py index 84d35f4bdd8..06de6bf0b59 100644 --- a/tests/components/cloud/test_http_api.py +++ b/tests/components/cloud/test_http_api.py @@ -111,6 +111,18 @@ def test_login_view(hass, cloud_client, mock_cognito): assert result_pass == 'my_password' +async def test_login_view_random_exception(cloud_client): + """Try logging in with invalid JSON.""" + with patch('async_timeout.timeout', side_effect=ValueError('Boom')): + req = await cloud_client.post('/api/cloud/login', json={ + 'email': 'my_username', + 'password': 'my_password' + }) + assert req.status == 502 + resp = await req.json() + assert resp == {'code': 'valueerror', 'message': 'Unexpected error: Boom'} + + @asyncio.coroutine def test_login_view_invalid_json(cloud_client): """Try logging in with invalid JSON."""