Skip to content

Commit

Permalink
Remove error wrapping (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob authored Feb 8, 2020
1 parent cbdc99b commit d429a7e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 32 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Aiohue

## Asynchronous library to control Philips Hue

Requires Python 3.5 and uses asyncio and aiohttp.
Expand Down Expand Up @@ -47,7 +48,7 @@ async def run(websession):
await group.set_action(on=not group.state['on'])


asyncio.get_event_loop().run_until_complete(main())
asyncio.run(main())
```

## Timeouts
Expand Down
26 changes: 6 additions & 20 deletions aiohue/bridge.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import asyncio

from aiohttp import client_exceptions

from .config import Config
from .groups import Groups
from .lights import Lights
from .scenes import Scenes
from .sensors import Sensors
from .errors import raise_error, ResponseError, RequestError
from .errors import raise_error


class Bridge:
Expand Down Expand Up @@ -64,21 +60,11 @@ async def request(self, method, path, json=None, auth=True):
url += '{}/'.format(self.username)
url += path

try:
async with self.websession.request(method, url, json=json) as res:
if res.status >= 300:
raise ResponseError(
'Server returned error code {}'.format(res.status))
if res.content_type != 'application/json':
raise ResponseError(
'Invalid content type: {}'.format(res.content_type))
data = await res.json()
_raise_on_error(data)
return data
except client_exceptions.ClientError as err:
raise RequestError(
'Error requesting data from {}: {}'.format(self.host, err)
) from None
async with self.websession.request(method, url, json=json) as res:
res.raise_for_status()
data = await res.json()
_raise_on_error(data)
return data


def _raise_on_error(data):
Expand Down
11 changes: 0 additions & 11 deletions aiohue/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,6 @@ class AiohueException(Exception):
"""Base error for aiohue."""


class RequestError(AiohueException):
"""Unable to fulfill request.
Raised when host or API cannot be reached.
"""


class ResponseError(AiohueException):
"""Invalid response."""


class Unauthorized(AiohueException):
"""Username is not authorized."""

Expand Down

0 comments on commit d429a7e

Please sign in to comment.