diff --git a/swh/web/api/throttling.py b/swh/web/api/throttling.py --- a/swh/web/api/throttling.py +++ b/swh/web/api/throttling.py @@ -1,4 +1,4 @@ -# Copyright (C) 2017-2020 The Software Heritage developers +# Copyright (C) 2017-2022 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU Affero General Public License version 3, or any later version # See top-level LICENSE file for more information @@ -6,13 +6,12 @@ from ipaddress import IPv4Network, IPv6Network, ip_address, ip_network from typing import Callable, List, TypeVar, Union -import sentry_sdk - from django.core.exceptions import ImproperlyConfigured import rest_framework from rest_framework.throttling import ScopedRateThrottle from swh.web.auth.utils import API_SAVE_ORIGIN_PERMISSION +from swh.web.common.exc import sentry_capture_exception from swh.web.config import get_config APIView = TypeVar("APIView", bound="rest_framework.views.APIView") @@ -109,7 +108,7 @@ request_allowed = super().allow_request(request, view) # use default rate limiting otherwise except ImproperlyConfigured as exc: - sentry_sdk.capture_exception(exc) + sentry_capture_exception(exc) setattr(view, self.scope_attr, default_scope) if request_allowed is None: diff --git a/swh/web/browse/utils.py b/swh/web/browse/utils.py --- a/swh/web/browse/utils.py +++ b/swh/web/browse/utils.py @@ -10,13 +10,12 @@ import chardet import magic -import sentry_sdk from django.utils.html import escape from django.utils.safestring import mark_safe from swh.web.common import archive, highlightjs -from swh.web.common.exc import NotFoundExc +from swh.web.common.exc import NotFoundExc, sentry_capture_exception from swh.web.common.utils import ( browsers_supported_image_mimes, django_cache, @@ -122,7 +121,7 @@ try: content_data = content_data.decode(enc).encode("utf-8") except Exception as exc: - sentry_sdk.capture_exception(exc) + sentry_capture_exception(exc) else: # ensure display in content view encoding = enc @@ -166,7 +165,7 @@ filetype = archive.lookup_content_filetype(query_string) language = archive.lookup_content_language(query_string) except Exception as exc: - sentry_sdk.capture_exception(exc) + sentry_capture_exception(exc) mimetype = "unknown" encoding = "unknown" if filetype: @@ -177,7 +176,7 @@ try: content_raw = archive.lookup_content_raw(query_string) except Exception as exc: - sentry_sdk.capture_exception(exc) + sentry_capture_exception(exc) raise NotFoundExc( "The bytes of the content are currently not available " "in the archive." diff --git a/swh/web/browse/views/content.py b/swh/web/browse/views/content.py --- a/swh/web/browse/views/content.py +++ b/swh/web/browse/views/content.py @@ -6,8 +6,6 @@ import difflib from distutils.util import strtobool -import sentry_sdk - from django.http import HttpResponse, JsonResponse from django.shortcuts import redirect, render @@ -22,7 +20,12 @@ request_content, ) from swh.web.common import archive, highlightjs, query -from swh.web.common.exc import BadInputExc, NotFoundExc, http_status_code_message +from swh.web.common.exc import ( + BadInputExc, + NotFoundExc, + http_status_code_message, + sentry_capture_exception, +) from swh.web.common.identifiers import get_swhids_info from swh.web.common.typing import ContentMetadata, SWHObjectInfo from swh.web.common.utils import gen_path_info, reverse, swh_object_icons @@ -162,7 +165,7 @@ diff_lines = difflib.unified_diff(content_from_lines, content_to_lines) diff_str = "".join(list(diff_lines)[2:]) except Exception as exc: - sentry_sdk.capture_exception(exc) + sentry_capture_exception(exc) diff_str = str(exc) diff_data["diff_str"] = diff_str diff --git a/swh/web/browse/views/directory.py b/swh/web/browse/views/directory.py --- a/swh/web/browse/views/directory.py +++ b/swh/web/browse/views/directory.py @@ -1,12 +1,10 @@ -# Copyright (C) 2017-2021 The Software Heritage developers +# Copyright (C) 2017-2022 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU Affero General Public License version 3, or any later version # See top-level LICENSE file for more information import os -import sentry_sdk - from django.http import HttpResponse from django.shortcuts import redirect, render @@ -15,7 +13,11 @@ from swh.web.browse.snapshot_context import get_snapshot_context from swh.web.browse.utils import gen_link, get_directory_entries, get_readme_to_display from swh.web.common import archive -from swh.web.common.exc import NotFoundExc, http_status_code_message +from swh.web.common.exc import ( + NotFoundExc, + http_status_code_message, + sentry_capture_exception, +) from swh.web.common.identifiers import get_swhids_info from swh.web.common.typing import DirectoryMetadata, SWHObjectInfo from swh.web.common.utils import gen_path_info, reverse, swh_object_icons @@ -288,5 +290,5 @@ ) return redirect(data_url) except Exception as exc: - sentry_sdk.capture_exception(exc) + sentry_capture_exception(exc) return HttpResponse(status=404) diff --git a/swh/web/browse/views/release.py b/swh/web/browse/views/release.py --- a/swh/web/browse/views/release.py +++ b/swh/web/browse/views/release.py @@ -3,8 +3,6 @@ # License: GNU Affero General Public License version 3, or any later version # See top-level LICENSE file for more information -import sentry_sdk - from django.shortcuts import render from swh.model.swhids import ObjectType @@ -19,7 +17,7 @@ gen_revision_link, ) from swh.web.common import archive -from swh.web.common.exc import NotFoundExc +from swh.web.common.exc import NotFoundExc, sentry_capture_exception from swh.web.common.identifiers import get_swhids_info from swh.web.common.typing import ReleaseMetadata, SWHObjectInfo from swh.web.common.utils import format_utc_iso_date, reverse @@ -132,7 +130,7 @@ SWHObjectInfo(object_type=ObjectType.DIRECTORY, object_id=rev_directory) ) except Exception as exc: - sentry_sdk.capture_exception(exc) + sentry_capture_exception(exc) elif release["target_type"] == ObjectType.DIRECTORY.name.lower(): target_link = gen_directory_link( release["target"], @@ -155,7 +153,7 @@ ) ) except Exception as exc: - sentry_sdk.capture_exception(exc) + sentry_capture_exception(exc) elif release["target_type"] == ObjectType.CONTENT.name.lower(): target_link = gen_content_link( release["target"], diff --git a/swh/web/common/highlightjs.py b/swh/web/common/highlightjs.py --- a/swh/web/common/highlightjs.py +++ b/swh/web/common/highlightjs.py @@ -8,10 +8,11 @@ from typing import Dict from pygments.lexers import get_all_lexers, get_lexer_for_filename -import sentry_sdk from django.contrib.staticfiles.finders import find +from swh.web.common.exc import sentry_capture_exception + @functools.lru_cache() def _hljs_languages_data(): @@ -140,7 +141,7 @@ try: lexer = get_lexer_for_filename(filename) except Exception as exc: - sentry_sdk.capture_exception(exc) + sentry_capture_exception(exc) # if there is a correspondence between the lexer and an hljs # language, return it if lexer and lexer.name in _pygments_lexer_to_hljs_language: diff --git a/swh/web/common/origin_save.py b/swh/web/common/origin_save.py --- a/swh/web/common/origin_save.py +++ b/swh/web/common/origin_save.py @@ -12,7 +12,6 @@ from prometheus_client import Gauge import requests -import sentry_sdk from django.core.exceptions import ObjectDoesNotExist, ValidationError from django.core.validators import URLValidator @@ -21,7 +20,12 @@ from swh.scheduler.utils import create_oneshot_task_dict from swh.web.common import archive -from swh.web.common.exc import BadInputExc, ForbiddenExc, NotFoundExc +from swh.web.common.exc import ( + BadInputExc, + ForbiddenExc, + NotFoundExc, + sentry_capture_exception, +) from swh.web.common.models import ( SAVE_REQUEST_ACCEPTED, SAVE_REQUEST_PENDING, @@ -810,7 +814,7 @@ except Exception as exc: logger.warning("Request to Elasticsearch failed\n%s", exc) - sentry_sdk.capture_exception(exc) + sentry_capture_exception(exc) if not full_info: for field in ("id", "backend_id", "worker"): diff --git a/swh/web/common/utils.py b/swh/web/common/utils.py --- a/swh/web/common/utils.py +++ b/swh/web/common/utils.py @@ -20,7 +20,6 @@ from prometheus_client.registry import CollectorRegistry import requests from requests.auth import HTTPBasicAuth -import sentry_sdk from django.core.cache import cache from django.core.cache.backends.base import DEFAULT_TIMEOUT @@ -34,7 +33,7 @@ ADMIN_LIST_DEPOSIT_PERMISSION, MAILMAP_ADMIN_PERMISSION, ) -from swh.web.common.exc import BadInputExc +from swh.web.common.exc import BadInputExc, sentry_capture_exception from swh.web.common.typing import QueryParameters from swh.web.config import SWH_WEB_SERVER_NAME, get_config, search @@ -434,8 +433,8 @@ try: ret = func(*args, **kwargs) except Exception as exc: - sentry_sdk.capture_exception(exc) if catch_exception: + sentry_capture_exception(exc) return exception_return_value else: raise diff --git a/swh/web/inbound_email/management/commands/process_inbound_email.py b/swh/web/inbound_email/management/commands/process_inbound_email.py --- a/swh/web/inbound_email/management/commands/process_inbound_email.py +++ b/swh/web/inbound_email/management/commands/process_inbound_email.py @@ -10,10 +10,9 @@ import sys from typing import Callable -import sentry_sdk - from django.core.management.base import BaseCommand +from swh.web.common.exc import sentry_capture_exception from swh.web.inbound_email import signals logger = logging.getLogger(__name__) @@ -27,7 +26,7 @@ try: message = email.message_from_bytes(raw_message, policy=email.policy.default) except Exception as exc: - sentry_sdk.capture_exception(exc) + sentry_capture_exception(exc) self.handle_failed_message(raw_message) # XXX make sure having logging doesn't make postfix unhappy logger.exception("Could not convert email from bytes") @@ -40,7 +39,7 @@ handled = False for receiver, response in responses: if isinstance(response, Exception): - sentry_sdk.capture_exception(response) + sentry_capture_exception(response) self.handle_failing_receiver(message, receiver) logger.error( "Receiver produced the following exception", exc_info=response diff --git a/swh/web/misc/urls.py b/swh/web/misc/urls.py --- a/swh/web/misc/urls.py +++ b/swh/web/misc/urls.py @@ -1,4 +1,4 @@ -# Copyright (C) 2019-2021 The Software Heritage developers +# Copyright (C) 2019-2022 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU Affero General Public License version 3, or any later version # See top-level LICENSE file for more information @@ -6,7 +6,6 @@ import json import requests -import sentry_sdk from django.conf.urls import include, url from django.contrib.staticfiles import finders @@ -14,6 +13,7 @@ from django.shortcuts import render from swh.web.common import archive +from swh.web.common.exc import sentry_capture_exception from swh.web.config import get_config from swh.web.misc.metrics import prometheus_metrics @@ -35,7 +35,7 @@ response = requests.get(url, timeout=5) stat_counters_history = json.loads(response.text) except Exception as exc: - sentry_sdk.capture_exception(exc) + sentry_capture_exception(exc) counters = { "stat_counters": stat_counters,