Update dump_devinfo to print original exception stack on errors. (#882)

This commit is contained in:
Steven B 2024-04-29 14:33:46 +01:00 committed by GitHub
parent e7553a7af4
commit 6724506fab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -15,6 +15,7 @@ import collections.abc
import json import json
import logging import logging
import re import re
import sys
import traceback import traceback
from collections import defaultdict, namedtuple from collections import defaultdict, namedtuple
from pathlib import Path from pathlib import Path
@ -343,6 +344,26 @@ def _echo_error(msg: str):
) )
def format_exception(e):
"""Print full exception stack as if it hadn't been caught.
https://stackoverflow.com/a/12539332
"""
exception_list = traceback.format_stack()
exception_list = exception_list[:-2]
exception_list.extend(traceback.format_tb(sys.exc_info()[2]))
exception_list.extend(
traceback.format_exception_only(sys.exc_info()[0], sys.exc_info()[1])
)
exception_str = "Traceback (most recent call last):\n"
exception_str += "".join(exception_list)
# Removing the last \n
exception_str = exception_str[:-1]
return exception_str
async def _make_requests_or_exit( async def _make_requests_or_exit(
device: SmartDevice, device: SmartDevice,
requests: list[SmartRequest], requests: list[SmartRequest],
@ -389,7 +410,7 @@ async def _make_requests_or_exit(
f"Unexpected exception querying {name} at once: {ex}", f"Unexpected exception querying {name} at once: {ex}",
) )
if _LOGGER.isEnabledFor(logging.DEBUG): if _LOGGER.isEnabledFor(logging.DEBUG):
traceback.print_stack() _echo_error(format_exception(ex))
exit(1) exit(1)
finally: finally:
await device.protocol.close() await device.protocol.close()