Skip to content

Failed transactions should raise appropriate exceptions #62

Description

@mill1000

The transact method should raise appropriate exceptions when failures occur. The current implementation catches everything, logs a warning and returns a generic error string.

async def _transact(self, method, params=None):
"""Wrap requests."""
result = error = None
try:
result, error = await self._protocol.request(method, params)
except:
_LOGGER.warning('could not send request')
error = 'could not send request'
return result or error

This makes detecting errors awkward as the type of the return is used to determine if a transaction was successful.
e.g.

result = await self._transact(method, params)
if isinstance(result, dict) and key in result:
return result.get(key)

I think it would be better for this method to raise the exception, possibly a custom one.

try:
    result, error = await self._protocol.request(method, params)
except OSError as e:
    _LOGGER.warning('Could not send request: %s', e)
    raise RequestException(e) from e

return result

Just an idea.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions