Improve cloud error handling (#20729)
* Improve cloud error handling * Lint
This commit is contained in:
parent
a3c8439ce2
commit
07b5b68a51
2 changed files with 18 additions and 3 deletions
|
@ -107,13 +107,16 @@ def _handle_cloud_errors(handler):
|
||||||
result = await handler(view, request, *args, **kwargs)
|
result = await handler(view, request, *args, **kwargs)
|
||||||
return result
|
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__)
|
err_info = _CLOUD_ERRORS.get(err.__class__)
|
||||||
if err_info is None:
|
if err_info is None:
|
||||||
|
_LOGGER.exception(
|
||||||
|
"Unexpected error processing request for %s", request.path)
|
||||||
err_info = (502, 'Unexpected error: {}'.format(err))
|
err_info = (502, 'Unexpected error: {}'.format(err))
|
||||||
status, msg = err_info
|
status, msg = err_info
|
||||||
return view.json_message(msg, status_code=status,
|
return view.json_message(
|
||||||
message_code=err.__class__.__name__)
|
msg, status_code=status,
|
||||||
|
message_code=err.__class__.__name__.lower())
|
||||||
|
|
||||||
return error_handler
|
return error_handler
|
||||||
|
|
||||||
|
|
|
@ -111,6 +111,18 @@ def test_login_view(hass, cloud_client, mock_cognito):
|
||||||
assert result_pass == 'my_password'
|
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
|
@asyncio.coroutine
|
||||||
def test_login_view_invalid_json(cloud_client):
|
def test_login_view_invalid_json(cloud_client):
|
||||||
"""Try logging in with invalid JSON."""
|
"""Try logging in with invalid JSON."""
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue