-
Notifications
You must be signed in to change notification settings - Fork 28
Description
It would be good to develop a consistent approach to logging, handling warnings and errors in Payu. Currently for errors, there's a range of:
sys.exit('payu: error: ...')- Standard exceptions: e.g.
raise RuntimeError(...),raise FileNotFoundError(...) - Custom exceptions:
raise PayuBranchError
For warnings:
print('payu: warning: ...')warnings.warn(...)
A benefit of using exceptions is that it is easy for them to be caught and handled. This is a large part why using sys.exit() is discouraged in library code. On the other hand, exceptions with stack traces (and warnings with code line numbers), aren't the most user friendly and it is often showing information that is not required. Though, especially in development, there are cases where it is useful to have stack traces.
An approach could be to catch exceptions in the main CLI entry point code, and then log the error messages and remove the stack trace, and have a clean exit with sys.exit(1). Similarly, the warnings could be configured in the main CLI entry point code to format and log the warnings. We could add a "debug" option flag to the CLI (and/or config.yaml) to include debug logs with stack traces.
A similar approach could be to do above - so keep any global configuration or wrappers around the CLI entry point code, but have custom PayuError type (with more specific sub-classes like PayuConfigError or PayuFileNotFound), then only these exceptions will have a clean error message with no stack trace, and all other exceptions keep their stack trace as before. This will have the benefit that we know the customs exceptions should have a descriptive error message.
Some references:
- An explanation of configuring global logging near the CLI entry point of code: https://realpython.com/ref/best-practices/logging/
- Blog post on error handling which goes into high level catching exceptions with a sys.exit(): https://blog.miguelgrinberg.com/post/the-ultimate-guide-to-error-handling-in-python
- An explanation on why it's difficult to handle system exits: https://stackoverflow.com/a/48571382
- Python docs on when to use logging: https://docs.python.org/3/howto/logging.html#when-to-use-logging