From 2bcb84211402bcd43f1cf343f8d0b61fdf373350 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 2 Jan 2019 19:37:05 +0100 Subject: [PATCH] Better sentry error reporting --- requirements.txt | 3 ++- statsupdate.py | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 7f22d54a..dea73f34 100644 --- a/requirements.txt +++ b/requirements.txt @@ -14,4 +14,5 @@ markovify praw dice raven[flask] -coloredlogs \ No newline at end of file +coloredlogs +sentry_sdk \ No newline at end of file diff --git a/statsupdate.py b/statsupdate.py index 5a3c956c..a1860197 100644 --- a/statsupdate.py +++ b/statsupdate.py @@ -8,7 +8,9 @@ import typing import telegram import sys import coloredlogs +import requests from dirty import Dirty +from sentry_sdk import configure_scope logging.getLogger().disabled = True logger = logging.getLogger(__name__) @@ -34,6 +36,23 @@ def update_block(session: db.Session, block: list, delay: float=0, change_callba t = time.clock() try: change = item.update(session=session) + except requests.exceptions.HTTPError as e: + with configure_scope() as scope: + if str(e.response.status_code).startswith("5"): + scope.level = "warning" + logger.warning(f"Server error {sys.exc_info()} while updating {repr(item)}.") + else: + scope.level = "error" + logger.error(f"Error {sys.exc_info()} while updating {repr(item)}.") + sentry.extra_context({ + "item": repr(item), + "response": { + "code": e.response.status_code, + "text": e.response.text + } + }) + sentry.captureException() + continue except Exception as e: logger.warning(f"Error {sys.exc_info()} while updating {repr(item)}.") sentry.extra_context({