Skip to content

Commit

Permalink
docs: add sample for TC401 (logging w/o object)
Browse files Browse the repository at this point in the history
  • Loading branch information
guilatrova committed Jul 31, 2021
1 parent 8ae2c28 commit 875ce09
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
7 changes: 4 additions & 3 deletions docs/violations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
## `TC4xx` - Logging usage


| Code | Description |
| ----------------- | ------------------------------------------- |
| [TC400](TC400.md) | Use logging '.exception' instead of 'error' |
| Code | Description |
| ----------------- | -------------------------------------------- |
| [TC400](TC400.md) | Use logging `.exception` instead of `.error` |
| [TC401](TC401.md) | Do not log the exception object |
29 changes: 29 additions & 0 deletions docs/violations/TC401.md
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
```

0 comments on commit 875ce09

Please sign in to comment.