Remove all instances of `SystemExit` and start implementing our own custom exceptions where needed
See original GitHub issueIs your feature request related to a specific problem?
Right now we use a lot of SystemExit
, but to maintain best practices for software development we should instead be creating custom exceptions everywhere that we would like to raise an exception.
Describe the solution you’d like
A single exceptions.py
file per module (fideslang
, fidesctl.core
, fidesctl.api
, fidesctl.connectors
, etc…) that contains the custom exceptions.
Describe alternatives you’ve considered, if any
Not using custom exceptions. Having the exceptions defined in the same place where they are used.
Additional context
N/A
Issue Analytics
- State:
- Created a year ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
Is there a way to prevent a SystemExit exception raised from ...
You can call os._exit() to directly exit, without throwing an exception: import os os._exit(1). This bypasses all of the python shutdown ...
Read more >How Python SystemExit Work with Examples - eduCBA
In Python, SystemExit is an exception that is raised by the sys.exit() method. In general, all exceptions must be derived from BaseException which...
Read more >Implement Custom Exceptions in Java: Why, When and How
Learn 4 best practices for custom exceptions in Java. Understand when and how to use them in your application.
Read more >Built-in Exceptions — Python 3.11.1 documentation
In Python, all exceptions must be instances of a class that derives from BaseException. In a try statement with an except clause that...
Read more >Exit function in Java | System.exit() Method with Example
Exception : It throws a SecurityException. Moving ahead with System.exit method(), let's see some of its practical implementation.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
There are several places in the codebase where the below concept is seen, but the inspiration for this issue came from the
check_server
function, introduced in #433:https://github.com/ethyca/fides/blob/ef0f636cf588b3edf2a6ba3ac5913fce198f83a5/src/fidesctl/cli/utils.py#L31-L53
It has four possible exit scenarios...
stdout
and raiseSystemExit(1)
with a stack trace.stdout
.stdout
.stdout
.The
check_server
function is currently referenced in two places:https://github.com/ethyca/fides/blob/ef0f636cf588b3edf2a6ba3ac5913fce198f83a5/src/fidesctl/cli/__init__.py#L91-L92
https://github.com/ethyca/fides/blob/ef0f636cf588b3edf2a6ba3ac5913fce198f83a5/src/fidesctl/cli/commands/util.py#L93-L104
In both of the above contexts, the code calling
check_server
is part of the “main” codepath.Uh... "main codepath"...? What?
Executing any CLI command directly calls thecli()
function, and socheck_server
is called prior to any command handler being executed (except for thestatus
command, which of course callscheck_server
itself).As a best practice, all logging and user feedback should occur within the main codepath directly, not within helper functions. To resolve this issue with respect to the above example only, I think an acceptable refactor could look like:
…and
APIServerUnavailableError
,InvalidAPIResponseError
, andAPICLIVersionMismatchError
should be implemented as custom exceptions with__str__()
methods that return useful error messages.The above code is incomplete, but with such a refactor
check_server
has exactly two exit scenarios: do nothing, orraise
an exception. Then, each “main” codepath handles the possible exceptions while producing log output and/or user feedback itself.The results are twofold:
This pattern should be repeated throughout the CLI codebase wherever applicable, as our default pattern for exception handling and providing user feedback. Separately, a resolution to #596 can replace
echo_red
andecho_green
with a library that helps maintain output consistency and enables true CLI logging to a local file, for debugging purposes.de-prioritizing this as it isn’t critical for the 1.7 release