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 traceback | import traceback | ||||
import sentry_sdk | import sentry_sdk | ||||
from django.http import HttpResponse | |||||
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 | ||||
class BadInputExc(ValueError): | class BadInputExc(ValueError): | ||||
▲ 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 handle_view_exception(request, exc, html_response=True): | 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() | ||||
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 | ||||
if html_response: | |||||
return _generate_error_page(request, error_code, error_description) | return _generate_error_page(request, error_code, error_description) | ||||
else: | |||||
return HttpResponse( | |||||
error_description, content_type="text/plain", status=error_code | |||||
) |