You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
>>> exc = FileNotFoundError('No such file or directory')
258
258
>>> get_exit_code_from_exception(exc)
259
-
66 # Exit code for FileNotFoundError
259
+
66
260
260
261
261
Testcase 2: Exception without a specific exit code.
262
262
>>> exc = Exception('Generic error')
263
263
>>> get_exit_code_from_exception(exc)
264
-
70 # Default exit code for internal software error
264
+
70
265
265
266
266
"""
267
+
iftype(exc) inEXCEPTION_EXIT_CODES:
268
+
returnEXCEPTION_EXIT_CODES[type(exc)]
267
269
forexc_classinEXCEPTION_EXIT_CODES:
268
270
ifisinstance(exc, exc_class):
269
271
returnEXCEPTION_EXIT_CODES[exc_class]
@@ -274,7 +276,10 @@ def exit_on_exception(func):
274
276
"""
275
277
Decorator that wraps a function to handle exceptions and exit with appropriate exit codes.
276
278
277
-
This decorator captures exceptions raised by the wrapped function and handles them by mapping them to predefined exit codes specified in `EXIT_CODES`. It ensures that both `SystemExit` exceptions (which may be raised by modules like `argparse`) and other base exceptions result in the program exiting with meaningful exit codes and error messages.
279
+
This decorator captures exceptions raised by the wrapped function and handles them by mapping
280
+
them to predefined exit codes specified in `EXIT_CODES`. It ensures that both `SystemExit`
281
+
exceptions (which may be raised by modules like `argparse`) and other base exceptions result
282
+
in the program exiting with meaningful exit codes and error messages.
278
283
279
284
Parameters:
280
285
func (callable): The function to be wrapped by the decorator.
0 commit comments