Skip to content

Commit

Permalink
fix: seperate FileNotFoundError and BrokenPipeError errors (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Aug 27, 2022
1 parent 5008c3f commit f0b3d81
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions src/bluetooth_adapters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import asyncio
import logging
from functools import cache
from pathlib import Path
from typing import Any

import async_timeout
Expand Down Expand Up @@ -46,8 +48,28 @@ async def _get_dbus_managed_objects() -> dict[str, Any]:
bus = await MessageBus(
bus_type=BusType.SYSTEM, negotiate_unix_fd=True
).connect()
except (FileNotFoundError, BrokenPipeError) as ex:
_LOGGER.debug("Dbus not available: %s", ex)
except FileNotFoundError as ex:
if is_docker_env():
_LOGGER.warning(
"DBus service not found; docker config may "
"be missing `-v /run/dbus:/run/dbus:ro`: %s",
ex,
)
_LOGGER.warning(
"DBus service not found; make sure the DBus socket " "is available: %s",
ex,
)
return {}
except BrokenPipeError as ex:
if is_docker_env():
_LOGGER.warning(
"DBus connection broken: %s; try restarting "
"`bluetooth`, `dbus`, and finally the docker container",
ex,
)
_LOGGER.warning(
"DBus connection broken: %s; try restarting " "`bluetooth` and `dbus`", ex
)
return {}
msg = Message(
destination="org.bluez",
Expand All @@ -59,11 +81,18 @@ async def _get_dbus_managed_objects() -> dict[str, Any]:
async with async_timeout.timeout(REPLY_TIMEOUT):
reply = await bus.call(msg)
except asyncio.TimeoutError:
_LOGGER.debug("Dbus timeout waiting for reply to GetManagedObjects")
_LOGGER.error(
"Dbus timeout waiting for reply to GetManagedObjects; try restarting "
"`bluetooth` and `dbus`"
)
return {}
bus.disconnect()
if not reply or reply.message_type != MessageType.METHOD_RETURN:
_LOGGER.debug("Unexpected replay: %s", reply)
_LOGGER.error(
"Received an unexpected reply from Dbus while "
"calling GetManagedObjects on org.bluez: %s",
reply,
)
return {}
results: dict[str, Any] = reply.body[0]
return results
Expand All @@ -87,3 +116,9 @@ def unpack_variants(dictionary: dict[str, Variant]) -> dict[str, Any]:
v = [x.value if isinstance(x, Variant) else x for x in v]
unpacked[k] = v
return unpacked


@cache
def is_docker_env() -> bool:
"""Return True if we run in a docker env."""
return Path("/.dockerenv").exists()

0 comments on commit f0b3d81

Please sign in to comment.