|
1 | 1 | #!/usr/bin/env python3
|
2 |
| -''' Gracefully exit after catching an exception. Call the |
3 |
| - graceful_exit function from within an except block that catches |
4 |
| - an error for which the intent is to exit the program without |
5 |
| - displaying full trace stack details. |
| 2 | +''' Utility module with functions for the namedtuple-maker application. |
| 3 | +
|
| 4 | + Usage: |
| 5 | + See individual function docstrings for usage instructions. |
| 6 | +''' |
| 7 | + |
| 8 | +# Imports - Python Standard Library |
| 9 | +from os import _exit |
| 10 | +from sys import exit, stderr |
| 11 | +from typing import AnyStr |
| 12 | + |
| 13 | + |
| 14 | +def graceful_exit( |
| 15 | + error_message: AnyStr = None, |
| 16 | + error_object: Exception = None |
| 17 | +): |
| 18 | + ''' Gracefully exit a program after catching an exception. Call the |
| 19 | + graceful_exit function from within a higher-level except block. |
| 20 | + Instead of using the raise keyword to display a stack trace and |
| 21 | + exit, graceful_exit will display a friendly message, and exit the |
| 22 | + program without displaying full trace stack details. |
6 | 23 |
|
7 | 24 | The graceful_exit function first attempts to use the sys.exit
|
8 | 25 | function to exit the application. If sys.exit raises a
|
9 | 26 | SystemExit exception, usually as the result of running a program
|
10 | 27 | within an interactive shell (IDLE, iPython, etc.), graceful_exit
|
11 | 28 | will use the os._exit function.
|
12 | 29 |
|
| 30 | + Args: |
| 31 | + error_message (AnyStr, optional): |
| 32 | + String error message to display. |
| 33 | +
|
| 34 | + error_object (Exception): |
| 35 | + Exception object from the source except block. Write |
| 36 | + the output to STDERR. |
| 37 | +
|
13 | 38 | Usage:
|
14 | 39 | # Step 1, import the graceful_exit() function into your
|
15 | 40 | # application.
|
|
27 | 52 | try:
|
28 | 53 | my_function()
|
29 | 54 | except NameError as error:
|
30 |
| - graceful_exit(error) |
31 |
| -''' |
32 |
| - |
33 |
| -# Imports - Python Standard Library |
34 |
| -from os import _exit |
35 |
| -from sys import exit, stderr |
36 |
| -from typing import AnyStr |
37 |
| - |
38 |
| - |
39 |
| -def graceful_exit( |
40 |
| - error_message: AnyStr = None, |
41 |
| - error_object: Exception = None |
42 |
| -): |
43 |
| - ''' Gracefully exit after catching an exception exception. Display |
44 |
| - a friendly exception message with the shorthand for repr(), and |
45 |
| - exit the application without displaying a full stack trace. |
46 |
| - Optionally write a custom error message to STDERR. |
47 |
| -
|
48 |
| - Args: |
49 |
| - error_message (AnyStr, optional): |
50 |
| - String error message to display. |
51 |
| -
|
52 |
| - error_object (Exception): |
53 |
| - Exception object from the source except block. |
| 55 | + graceful_exit( |
| 56 | + error_message='An error occurred', |
| 57 | + error_object=error |
| 58 | + ) |
54 | 59 |
|
55 | 60 | Returns:
|
56 | 61 | N/A.
|
|
0 commit comments