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 traceback | import traceback | ||||
import sentry_sdk | import sentry_sdk | ||||
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.Logger(__name__) | |||||
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. | ||||
""" | """ | ||||
▲ Show 20 Lines • Show All 106 Lines • ▼ Show 20 Lines | 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_sdk.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.error(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 |