improve logging in case of exception

This commit is contained in:
mampfes
2022-12-29 15:12:05 +01:00
parent 762c8fd80b
commit e774a351d6
2 changed files with 16 additions and 2 deletions

View File

@@ -25,6 +25,12 @@ def main():
parser.add_argument(
"-i", "--icon", action="store_true", help="Show waste type icon"
)
parser.add_argument(
"-t",
"--traceback",
action="store_true",
help="Print exception information and stack trace",
)
args = parser.parse_args()
# read secrets.yaml
@@ -97,8 +103,10 @@ def main():
print(f" {x.date.isoformat()}: {x.type}{icon_str}")
except KeyboardInterrupt:
exit()
except Exception:
print(traceback.format_exc())
except Exception as exc:
print(f" {name} failed with exception: {exc}")
if args.traceback:
print(indent(traceback.format_exc(), 4))
def replace_secret(secrets, d):
@@ -116,5 +124,10 @@ def replace_secret(secrets, d):
print(f"identifier '{id}' not found in {SECRET_FILENAME}")
def indent(s, count):
indent = " " * count
return "\n".join([indent + line for line in s.split("\n")])
if __name__ == "__main__":
main()

View File

@@ -218,6 +218,7 @@ The script supports the following options:
| `-s` | SOURCE | [Source name](https://github.com/mampfes/hacs_waste_collection_schedule#source-configuration-variables) (source file name without ending `.py`) |
| `-l` | - | List all found dates. |
| `-i` | - | Add icon name to output. Only effective together with `-l`. |
| `-t` | - | Show extended exception info and stack trace. |
For debugging purposes of a single source, it is recommended to use the `-s SOURCE` option. If used without any arguments provided, the script tests every script in the `custom_components/waste_collection_schedule/waste_collection_schedule/source` folder and prints the number of found entries for every test case.