-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add sample for TC401 (logging w/o object)
- Loading branch information
1 parent
8ae2c28
commit 875ce09
Showing
2 changed files
with
33 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# `TC401` - Do not log the exception object | ||
|
||
## Why is it bad | ||
|
||
It's verbose, by using `logging.exception` you're already getting the exception message. Use the message to give context instead. | ||
|
||
## How it looks like | ||
|
||
```py | ||
def main_function(): | ||
try: | ||
process() | ||
handle() | ||
finish() | ||
except Exception as ex: | ||
logger.exception(f"Found an error: {ex}") # 'ex' message is going to be displayed twice | ||
``` | ||
|
||
## How it should be | ||
|
||
```py | ||
def main_function(): | ||
try: | ||
process() | ||
handle() | ||
finish() | ||
except Exception: # <- No need to get object | ||
logger.exception("Something failed during main_function") # <- Message will be shown alongside stack trace | ||
``` |