restore proper error printing

Error represenation should be part of the error and we should not repeat
formatting here.
if we are in debug mode also print the stack trace
This commit is contained in:
Jörg Thalheim
2024-10-08 11:44:25 +02:00
committed by Mic92
parent 46ceb62658
commit 68271b4d12

View File

@@ -24,7 +24,7 @@ from . import (
from .clan_uri import FlakeId from .clan_uri import FlakeId
from .custom_logger import setup_logging from .custom_logger import setup_logging
from .dirs import get_clan_flake_toplevel_or_env from .dirs import get_clan_flake_toplevel_or_env
from .errors import ClanCmdError, ClanError from .errors import ClanError
from .facts import cli as facts from .facts import cli as facts
from .flash import cli as flash_cli from .flash import cli as flash_cli
from .hyperlink import help_hyperlink from .hyperlink import help_hyperlink
@@ -401,7 +401,7 @@ def main() -> None:
if len(sys.argv) == 1: if len(sys.argv) == 1:
parser.print_help() parser.print_help()
if getattr(args, "debug", False): if debug := getattr(args, "debug", False):
setup_logging(logging.DEBUG, root_log_name=__name__.split(".")[0]) setup_logging(logging.DEBUG, root_log_name=__name__.split(".")[0])
log.debug("Debug log activated") log.debug("Debug log activated")
else: else:
@@ -413,21 +413,11 @@ def main() -> None:
try: try:
args.func(args) args.func(args)
except ClanError as e: except ClanError as e:
if isinstance(e, ClanCmdError): if debug:
msg = "" log.exception("Exited with error")
if e.cmd.msg: else:
msg += f"{e.cmd.msg}: " log.error("%s", e) # noqa: TRY400
msg += f"command exited with code {e.cmd.returncode}: {e.cmd.command}"
log.error(msg) # noqa: TRY400
sys.exit(1) sys.exit(1)
if not e.msg: # should not be empty, print stack trace
raise
msg = e.msg
if e.description:
msg += f": {e.description}"
log.error(msg) # noqa: TRY400
sys.exit(1)
except KeyboardInterrupt: except KeyboardInterrupt:
log.warning("Interrupted by user") log.warning("Interrupted by user")
sys.exit(1) sys.exit(1)