Improve cloud error handling (#20729)

* Improve cloud error handling

* Lint
This commit is contained in:
Paulus Schoutsen 2019-02-04 01:14:30 -08:00 committed by Pascal Vizeli
parent a3c8439ce2
commit 07b5b68a51
2 changed files with 18 additions and 3 deletions

View file

@ -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

View file

@ -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."""