Always log exception using logging.exception

This commit is contained in:
Josh Wu
2025-08-18 15:38:43 +08:00
parent 3b8dd6f3cf
commit 4a88e9a0cf
16 changed files with 65 additions and 74 deletions

View File

@@ -230,8 +230,8 @@ class SoundDeviceAudioOutput(ThreadedAudioOutput):
try:
self._stream.write(pcm_samples)
except Exception as error:
print(f'Sound device error: {error}')
except Exception:
logger.exception('Sound device error')
raise
def _close(self):

View File

@@ -137,8 +137,8 @@ class MessageAssembler:
self.pid,
self.payload,
)
except Exception as error:
logger.exception(color(f"!!! exception in callback: {error}", "red"))
except Exception:
logger.exception(color("!!! exception in callback", "red"))
self.reset()

View File

@@ -434,8 +434,8 @@ class MessageAssembler:
)
try:
self.callback(self.transaction_label, message)
except Exception as error:
logger.exception(color(f'!!! exception in callback: {error}', 'red'))
except Exception:
logger.exception(color('!!! exception in callback', 'red'))
self.reset()
@@ -1400,10 +1400,8 @@ class Protocol(utils.EventEmitter):
try:
response = handler(message)
self.send_message(transaction_label, response)
except Exception as error:
logger.warning(
f'{color("!!! Exception in handler:", "red")} {error}'
)
except Exception:
logger.exception(color("!!! Exception in handler:", "red"))
else:
logger.warning('unhandled command')
else:

View File

@@ -267,8 +267,8 @@ class PduAssembler:
assert self.pdu_id is not None
try:
self.callback(self.pdu_id, self.parameter)
except Exception as error:
logger.exception(color(f'!!! exception in callback: {error}', 'red'))
except Exception:
logger.exception(color('!!! exception in callback', 'red'))
self.reset()

View File

@@ -3242,8 +3242,8 @@ class Device(utils.CompositeEventEmitter):
else 0
)
await advertising_set.start(duration=duration)
except Exception as error:
logger.exception(f'failed to start advertising set: {error}')
except Exception:
logger.exception('failed to start advertising set')
await advertising_set.remove()
raise
@@ -4607,8 +4607,8 @@ class Device(utils.CompositeEventEmitter):
try:
await self.keystore.update(address, keys)
await self.refresh_resolving_list()
except Exception as error:
logger.warning(f'!!! error while storing keys: {error}')
except Exception:
logger.exception('!!! error while storing keys')
else:
self.emit(self.EVENT_KEY_STORE_UPDATE)
@@ -5856,8 +5856,8 @@ class Device(utils.CompositeEventEmitter):
)
)
return
except Exception as error:
logger.warning(f'exception while confirming: {error}')
except Exception:
logger.exception('exception while confirming')
await self.host.send_command(
hci.HCI_User_Confirmation_Request_Negative_Reply_Command(
@@ -5886,8 +5886,8 @@ class Device(utils.CompositeEventEmitter):
)
)
return
except Exception as error:
logger.warning(f'exception while asking for pass-key: {error}')
except Exception:
logger.exception('exception while asking for pass-key')
await self.host.send_command(
hci.HCI_User_Passkey_Request_Negative_Reply_Command(

View File

@@ -507,15 +507,15 @@ class Server(utils.EventEmitter):
error_code=error.error_code,
)
self.send_response(connection, response)
except Exception as error:
logger.warning(f'{color("!!! Exception in handler:", "red")} {error}')
except Exception:
logger.exception(color("!!! Exception in handler:", "red"))
response = att.ATT_Error_Response(
request_opcode_in_error=att_pdu.op_code,
attribute_handle_in_error=0x0000,
error_code=att.ATT_UNLIKELY_ERROR_ERROR,
)
self.send_response(connection, response)
raise error
raise
else:
# No specific handler registered
if att_pdu.op_code in att.ATT_REQUESTS:
@@ -982,8 +982,8 @@ class Server(utils.EventEmitter):
# Accept the value
try:
await attribute.write_value(connection, request.attribute_value)
except Exception as error:
logger.exception(f'!!! ignoring exception: {error}')
except Exception:
logger.exception('!!! ignoring exception')
def on_att_handle_value_confirmation(
self,

View File

@@ -707,11 +707,9 @@ class Host(utils.EventEmitter):
raise hci.HCI_Error(status)
return response
except Exception as error:
logger.exception(
f'{color("!!! Exception while sending command:", "red")} {error}'
)
raise error
except Exception:
logger.exception(color("!!! Exception while sending command:", "red"))
raise
finally:
self.pending_command = None
self.pending_response = None

View File

@@ -1695,8 +1695,8 @@ class ChannelManager:
if handler:
try:
handler(connection, cid, control_frame)
except Exception as error:
logger.warning(f'{color("!!! Exception in handler:", "red")} {error}')
except Exception:
logger.exception(color("!!! Exception in handler:", "red"))
self.send_control_frame(
connection,
cid,
@@ -1706,7 +1706,7 @@ class ChannelManager:
data=b'',
),
)
raise error
raise
else:
logger.error(color('Channel Manager command not handled???', 'red'))
self.send_control_frame(
@@ -2192,8 +2192,8 @@ class ChannelManager:
# Connect
try:
await channel.connect()
except Exception as error:
logger.warning(f'connection failed: {error}')
except Exception:
logger.exception('connection failed')
del connection_channels[source_cid]
raise

View File

@@ -1047,8 +1047,8 @@ class Client:
self.l2cap_channel = await self.connection.create_l2cap_channel(
spec=l2cap.ClassicChannelSpec(psm=RFCOMM_PSM, mtu=self.l2cap_mtu)
)
except ProtocolError as error:
logger.warning(f'L2CAP connection failed: {error}')
except ProtocolError:
logger.exception('L2CAP connection failed')
raise
assert self.l2cap_channel is not None

View File

@@ -1084,8 +1084,8 @@ class Server:
def on_pdu(self, pdu):
try:
sdp_pdu = SDP_PDU.from_bytes(pdu)
except Exception as error:
logger.warning(color(f'failed to parse SDP Request PDU: {error}', 'red'))
except Exception:
logger.exception(color('failed to parse SDP Request PDU', 'red'))
self.send_response(
SDP_ErrorResponse(
transaction_id=0, error_code=SDP_INVALID_REQUEST_SYNTAX_ERROR
@@ -1100,8 +1100,8 @@ class Server:
if handler:
try:
handler(sdp_pdu)
except Exception as error:
logger.exception(f'{color("!!! Exception in handler:", "red")} {error}')
except Exception:
logger.exception(color("!!! Exception in handler:", "red"))
self.send_response(
SDP_ErrorResponse(
transaction_id=sdp_pdu.transaction_id,

View File

@@ -882,8 +882,8 @@ class Session:
if response:
next_steps()
return
except Exception as error:
logger.warning(f'exception while confirm: {error}')
except Exception:
logger.exception('exception while confirm')
self.send_pairing_failed(SMP_CONFIRM_VALUE_FAILED_ERROR)
@@ -901,8 +901,8 @@ class Session:
if response:
next_steps()
return
except Exception as error:
logger.warning(f'exception while prompting: {error}')
except Exception:
logger.exception('exception while prompting')
self.send_pairing_failed(SMP_CONFIRM_VALUE_FAILED_ERROR)
@@ -919,8 +919,8 @@ class Session:
return
logger.debug(f'user input: {passkey}')
next_steps(passkey)
except Exception as error:
logger.warning(f'exception while prompting: {error}')
except Exception:
logger.exception('exception while prompting')
self.send_pairing_failed(SMP_PASSKEY_ENTRY_FAILED_ERROR)
self.connection.cancel_on_disconnection(prompt())
@@ -968,8 +968,8 @@ class Session:
try:
self.connection.cancel_on_disconnection(display_passkey())
except Exception as error:
logger.warning(f'exception while displaying passkey: {error}')
except Exception:
logger.exception('exception while displaying passkey')
else:
self.input_passkey(next_steps)
@@ -1414,8 +1414,8 @@ class Session:
if handler is not None:
try:
handler(command)
except Exception as error:
logger.exception(f'{color("!!! Exception in handler:", "red")} {error}')
except Exception:
logger.exception(color("!!! Exception in handler:", "red"))
response = SMP_Pairing_Failed_Command(
reason=SMP_UNSPECIFIED_REASON_ERROR
)
@@ -1436,8 +1436,8 @@ class Session:
# Check if the request should proceed
try:
accepted = await self.pairing_config.delegate.accept()
except Exception as error:
logger.warning(f'exception while accepting: {error}')
except Exception:
logger.exception('exception while accepting')
accepted = False
if not accepted:
logger.debug('pairing rejected by delegate')

View File

@@ -48,8 +48,8 @@ def _wrap_transport(transport: Transport) -> Transport:
return SnoopingTransport.create_with(
transport, create_snooper(snooper_spec)
)
except Exception as exc:
logger.warning(f'Exception while creating snooper: {exc}')
except Exception:
logger.exception('Exception while creating snooper')
return transport

View File

@@ -90,8 +90,8 @@ class PacketPump:
try:
# Deliver the packet to the sink
self.sink.on_packet(await self.reader.next_packet())
except Exception as error:
logger.warning(f'!!! {error}')
except Exception:
logger.exception('!!!')
# -----------------------------------------------------------------------------
@@ -158,10 +158,8 @@ class PacketParser:
if self.sink:
try:
self.sink.on_packet(bytes(self.packet))
except Exception as error:
logger.exception(
color(f'!!! Exception in on_packet: {error}', 'red')
)
except Exception:
logger.exception(color('!!! Exception in on_packet', 'red'))
self.reset()
def set_packet_sink(self, sink: TransportSink) -> None:
@@ -378,7 +376,7 @@ class PumpedPacketSource(ParserSource):
self.terminated.set_result(None)
break
except Exception as error:
logger.warning(f'exception while waiting for packet: {error}')
logger.exception('exception while waiting for packet')
if not self.terminated.done():
self.terminated.set_exception(error)
break
@@ -409,8 +407,8 @@ class PumpedPacketSink:
except asyncio.CancelledError:
logger.debug('sink pump task done')
break
except Exception as error:
logger.warning(f'exception while sending packet: {error}')
except Exception:
logger.exception('exception while sending packet')
break
self.pump_task = asyncio.create_task(pump_packets())

View File

@@ -285,7 +285,7 @@ async def open_pyusb_transport(spec: str) -> Transport:
try:
device = await _power_cycle(device) # type: ignore
except Exception as e:
logging.debug(e)
logging.debug(e, stack_info=True)
logging.info(f"Unable to power cycle {hex(device.idVendor)} {hex(device.idProduct)}") # type: ignore
# Collect the metadata
@@ -371,9 +371,8 @@ async def _power_cycle(device: UsbDevice) -> UsbDevice:
# Device needs to be find again otherwise it will appear as disconnected
return usb.core.find(idVendor=device.idVendor, idProduct=device.idProduct) # type: ignore
except USBError as e:
logger.error(f"Adjustment needed: Please revise the udev rule for device {hex(device.idVendor)}:{hex(device.idProduct)} for proper recognition.") # type: ignore
logger.error(e)
except USBError:
logger.exception(f"Adjustment needed: Please revise the udev rule for device {hex(device.idVendor)}:{hex(device.idProduct)} for proper recognition.") # type: ignore
return device

View File

@@ -292,9 +292,9 @@ async def open_usb_transport(spec: str) -> Transport:
if self.sink:
try:
self.sink.on_packet(packet)
except Exception as error:
except Exception:
logger.exception(
color(f'!!! Exception in sink.on_packet: {error}', 'red')
color('!!! Exception in sink.on_packet', 'red')
)
def close(self):

View File

@@ -317,10 +317,8 @@ class AsyncRunner:
item = await self.queue.get()
try:
await item
except Exception as error:
logger.warning(
f'{color("!!! Exception in work queue:", "red")} {error}'
)
except Exception:
logger.exception(color("!!! Exception in work queue", "red"))
# Shared default queue
default_queue = WorkQueue()