diff --git a/swh/web/api/apiresponse.py b/swh/web/api/apiresponse.py --- a/swh/web/api/apiresponse.py +++ b/swh/web/api/apiresponse.py @@ -8,8 +8,6 @@ import traceback from typing import Any, Dict, Optional -import sentry_sdk - from django.http import HttpResponse from django.shortcuts import render from django.utils.cache import add_never_cache_headers @@ -21,7 +19,13 @@ from swh.storage.exc import StorageAPIError, StorageDBError from swh.web.api import utils -from swh.web.common.exc import BadInputExc, ForbiddenExc, LargePayloadExc, NotFoundExc +from swh.web.common.exc import ( + BadInputExc, + ForbiddenExc, + LargePayloadExc, + NotFoundExc, + sentry_capture_exception, +) from swh.web.common.utils import gen_path_info, shorten_path from swh.web.config import get_config @@ -220,6 +224,6 @@ ) -> Optional[HttpResponse]: """Custom DRF exception handler used to generate API error responses. """ - sentry_sdk.capture_exception(exc) + sentry_capture_exception(exc) doc_data = getattr(exc, "doc_data", None) return error_response(context["request"], exc, doc_data) diff --git a/swh/web/common/exc.py b/swh/web/common/exc.py --- a/swh/web/common/exc.py +++ b/swh/web/common/exc.py @@ -8,6 +8,7 @@ import sentry_sdk +from django.core import exceptions from django.shortcuts import render from django.utils.html import escape from django.utils.safestring import mark_safe @@ -27,7 +28,7 @@ pass -class NotFoundExc(Exception): +class NotFoundExc(exceptions.ObjectDoesNotExist): """Good request to the api but no result were found. Example: Asking a content with the right identifier format but @@ -38,7 +39,7 @@ pass -class ForbiddenExc(Exception): +class ForbiddenExc(exceptions.PermissionDenied): """Good request to the api, forbidden result to return due to enforce policy. @@ -128,12 +129,24 @@ return _generate_error_page(request, 500, error_description) +def sentry_capture_exception(exc): + if not isinstance( + exc, + ( + exceptions.ObjectDoesNotExist, + exceptions.DisallowedHost, + exceptions.PermissionDenied, + ), + ): + sentry_sdk.capture_exception(exc) + + def handle_view_exception(request, exc): """ Function used to generate an error page when an exception was raised inside a swh-web browse view. """ - sentry_sdk.capture_exception(exc) + sentry_capture_exception(exc) error_code = 500 error_description = "%s: %s" % (type(exc).__name__, str(exc)) if get_config()["debug"]: diff --git a/swh/web/common/middlewares.py b/swh/web/common/middlewares.py --- a/swh/web/common/middlewares.py +++ b/swh/web/common/middlewares.py @@ -5,9 +5,8 @@ from htmlmin import minify -import sentry_sdk -from swh.web.common.exc import handle_view_exception +from swh.web.common.exc import handle_view_exception, sentry_capture_exception from swh.web.common.utils import prettify_html @@ -51,7 +50,7 @@ ) response.content = minified_html.encode("utf-8") except Exception as exc: - sentry_sdk.capture_exception(exc) + sentry_capture_exception(exc) return response