Skip to content

Commit

Permalink
Add global exception handler to nitropy command
Browse files Browse the repository at this point in the history
This patch introduces a wrapper around the nitropy command,
pynitrokey.cli.main, that catches unhandled exceptions and uses
local_critical to display them to the user.  For more consistency, we
also move the CliException handling to that wrapper.

Note that this changes the main entry point from pynitrokey.cli.nitropy
to pynitrokey.cli.main, so developers have to run `make update-venv` to
apply this change.

Fixes #193
  • Loading branch information
robin-nitrokey committed Feb 10, 2022
1 parent 74c5280 commit fe24170
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
6 changes: 2 additions & 4 deletions nitropy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
# #print (os.environ["PATH"])


from pynitrokey.cli import nitropy

nitropy()

from pynitrokey.cli import main

main()
15 changes: 15 additions & 0 deletions pynitrokey/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
#
# Copyright 2019 SoloKeys Developers
# Copyright 2022 Nitrokey Developers
#
# Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
# http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
Expand All @@ -16,16 +17,20 @@

import pynitrokey
import pynitrokey.fido2.operations
from pynitrokey.cli.exceptions import CliException
from pynitrokey.cli.fido2 import fido2
from pynitrokey.cli.nethsm import nethsm
from pynitrokey.cli.nk3 import nk3
from pynitrokey.cli.pro import pro
from pynitrokey.cli.start import start
from pynitrokey.cli.storage import storage
from pynitrokey.confconsts import LOG_FN, LOG_FORMAT
from pynitrokey.helpers import local_critical

# from . import _patches # noqa (since otherwise "unused")

logger = logging.getLogger(__name__)


def check_root():
if (os.name == "posix") and os.environ.get("ALLOW_ROOT") is None:
Expand Down Expand Up @@ -93,3 +98,13 @@ def ls():

nitropy.add_command(list)
nitropy.add_command(ls)


def main() -> None:
try:
nitropy()
except CliException as e:
e.show()
except Exception as e:
logger.warning("An unhandled exception occured", exc_info=True)
local_critical("An unhandled exception occured", e)
4 changes: 1 addition & 3 deletions pynitrokey/cli/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
# http://opensource.org/licenses/MIT>, at your option. This file may not be
# copied, modified, or distributed except according to those terms.

import click

from pynitrokey.helpers import local_critical


class CliException(click.ClickException):
class CliException(Exception):
def __init__(
self, *messages, support_hint: bool = True, ret_code: int = 1, **kwargs
):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ dev = [
Source = "https://github.com/Nitrokey/pynitrokey"

[project.scripts]
nitropy = "pynitrokey.cli:nitropy"
nitropy = "pynitrokey.cli:main"

[tool.isort]
profile = "black"

0 comments on commit fe24170

Please sign in to comment.