Changeset View
Changeset View
Standalone View
Standalone View
swh/web/common/exc.py
# Copyright (C) 2015-2019 The Software Heritage developers | # Copyright (C) 2015-2022 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import logging | import logging | ||||
import traceback | import traceback | ||||
import sentry_sdk | import sentry_sdk | ||||
from django.core import exceptions | from django.core import exceptions | ||||
from django.shortcuts import render | from django.shortcuts import render | ||||
from django.utils.html import escape | from django.utils.html import escape | ||||
from django.utils.safestring import mark_safe | from django.utils.safestring import mark_safe | ||||
from rest_framework.exceptions import AuthenticationFailed | from rest_framework.exceptions import APIException | ||||
from swh.web.config import get_config | from swh.web.config import get_config | ||||
logger = logging.getLogger("django") | logger = logging.getLogger("django") | ||||
class BadInputExc(ValueError): | class BadInputExc(ValueError): | ||||
"""Wrong request to the api. | """Wrong request to the api. | ||||
▲ Show 20 Lines • Show All 102 Lines • ▼ Show 20 Lines | def swh_handle500(request): | ||||
error_description = ( | error_description = ( | ||||
"An unexpected condition was encountered when " | "An unexpected condition was encountered when " | ||||
"requesting resource %s." % escape(request.META["PATH_INFO"]) | "requesting resource %s." % escape(request.META["PATH_INFO"]) | ||||
) | ) | ||||
return _generate_error_page(request, 500, error_description) | return _generate_error_page(request, 500, error_description) | ||||
def sentry_capture_exception(exc): | def sentry_capture_exception(exc): | ||||
if not isinstance( | if isinstance( | ||||
exc, | exc, | ||||
( | ( | ||||
exceptions.ObjectDoesNotExist, | exceptions.ObjectDoesNotExist, | ||||
exceptions.DisallowedHost, | exceptions.DisallowedHost, | ||||
exceptions.PermissionDenied, | exceptions.PermissionDenied, | ||||
BadInputExc, | BadInputExc, | ||||
NotFoundExc, | NotFoundExc, | ||||
AuthenticationFailed, | |||||
), | ), | ||||
): | ): | ||||
# ignore noisy exceptions we cannot do anything about | |||||
pass | |||||
elif isinstance(exc, APIException) and 400 <= exc.status_code < 500: | |||||
# ignore client errors (4xx status codes) | |||||
pass | |||||
ardumont: ? | |||||
vlorentzAuthorUnsubmitted Done Inline ActionsI think it is more readable to keep the two cases separate, each with its own comment vlorentz: I think it is more readable to keep the two cases separate, each with its own comment | |||||
else: | |||||
# log everything else | |||||
sentry_sdk.capture_exception(exc) | sentry_sdk.capture_exception(exc) | ||||
def handle_view_exception(request, exc): | def handle_view_exception(request, exc): | ||||
""" | """ | ||||
Function used to generate an error page when an exception | Function used to generate an error page when an exception | ||||
was raised inside a swh-web browse view. | was raised inside a swh-web browse view. | ||||
""" | """ | ||||
Show All 17 Lines |
?