Changeset View
Changeset View
Standalone View
Standalone View
swh/web/common/exc.py
# Copyright (C) 2015-2019 The Software Heritage developers | # Copyright (C) 2015-2019 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.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 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. | ||||
Example: Asking a content with the wrong identifier format. | Example: Asking a content with the wrong identifier format. | ||||
""" | """ | ||||
pass | pass | ||||
class NotFoundExc(Exception): | class NotFoundExc(exceptions.ObjectDoesNotExist): | ||||
"""Good request to the api but no result were found. | """Good request to the api but no result were found. | ||||
Example: Asking a content with the right identifier format but | Example: Asking a content with the right identifier format but | ||||
that content does not exist. | that content does not exist. | ||||
""" | """ | ||||
pass | pass | ||||
class ForbiddenExc(Exception): | class ForbiddenExc(exceptions.PermissionDenied): | ||||
"""Good request to the api, forbidden result to return due to enforce | """Good request to the api, forbidden result to return due to enforce | ||||
policy. | policy. | ||||
Example: Asking for a raw content which exists but whose mimetype | Example: Asking for a raw content which exists but whose mimetype | ||||
is not text. | is not text. | ||||
""" | """ | ||||
▲ Show 20 Lines • Show All 73 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): | |||||
if not isinstance( | |||||
exc, | |||||
( | |||||
exceptions.ObjectDoesNotExist, | |||||
exceptions.DisallowedHost, | |||||
exceptions.PermissionDenied, | |||||
), | |||||
): | |||||
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. | ||||
""" | """ | ||||
sentry_sdk.capture_exception(exc) | sentry_capture_exception(exc) | ||||
error_code = 500 | error_code = 500 | ||||
error_description = "%s: %s" % (type(exc).__name__, str(exc)) | error_description = "%s: %s" % (type(exc).__name__, str(exc)) | ||||
if get_config()["debug"]: | if get_config()["debug"]: | ||||
error_description = traceback.format_exc() | error_description = traceback.format_exc() | ||||
logger.debug(error_description) | logger.debug(error_description) | ||||
if isinstance(exc, BadInputExc): | if isinstance(exc, BadInputExc): | ||||
error_code = 400 | error_code = 400 | ||||
if isinstance(exc, ForbiddenExc): | if isinstance(exc, ForbiddenExc): | ||||
error_code = 403 | error_code = 403 | ||||
if isinstance(exc, NotFoundExc): | if isinstance(exc, NotFoundExc): | ||||
error_code = 404 | error_code = 404 | ||||
resp = _generate_error_page(request, error_code, error_description) | resp = _generate_error_page(request, error_code, error_description) | ||||
if get_config()["debug"]: | if get_config()["debug"]: | ||||
resp.traceback = error_description | resp.traceback = error_description | ||||
return resp | return resp |