Changeset View
Changeset View
Standalone View
Standalone View
swh/web/common/exc.py
# Copyright (C) 2015-2018 The Software Heritage developers | # Copyright (C) 2015-2018 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 | ||||
from django.http import HttpResponse | from django.http import HttpResponse | ||||
from django.shortcuts import render | from django.shortcuts import render | ||||
from django.utils.safestring import mark_safe | from django.utils.safestring import mark_safe | ||||
from django.utils.html import escape | |||||
from swh.web.config import get_config | from swh.web.config import get_config | ||||
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 44 Lines • ▼ Show 20 Lines | |||||
def swh_handle400(request): | def swh_handle400(request): | ||||
""" | """ | ||||
Custom Django HTTP error 400 handler for swh-web. | Custom Django HTTP error 400 handler for swh-web. | ||||
""" | """ | ||||
error_description = 'The server cannot process the request to %s due to '\ | error_description = 'The server cannot process the request to %s due to '\ | ||||
'something that is perceived to be a client error.' %\ | 'something that is perceived to be a client error.' %\ | ||||
request.META['PATH_INFO'] | escape(request.META['PATH_INFO']) | ||||
return _generate_error_page(request, 400, error_description) | return _generate_error_page(request, 400, error_description) | ||||
def swh_handle403(request): | def swh_handle403(request): | ||||
""" | """ | ||||
Custom Django HTTP error 403 handler for swh-web. | Custom Django HTTP error 403 handler for swh-web. | ||||
""" | """ | ||||
error_description = 'The resource %s requires an authentication.' %\ | error_description = 'The resource %s requires an authentication.' %\ | ||||
request.META['PATH_INFO'] | escape(request.META['PATH_INFO']) | ||||
return _generate_error_page(request, 403, error_description) | return _generate_error_page(request, 403, error_description) | ||||
def swh_handle404(request): | def swh_handle404(request): | ||||
""" | """ | ||||
Custom Django HTTP error 404 handler for swh-web. | Custom Django HTTP error 404 handler for swh-web. | ||||
""" | """ | ||||
error_description = 'The resource %s could not be found on the server.' %\ | error_description = 'The resource %s could not be found on the server.' %\ | ||||
request.META['PATH_INFO'] | escape(request.META['PATH_INFO']) | ||||
return _generate_error_page(request, 404, error_description) | return _generate_error_page(request, 404, error_description) | ||||
def swh_handle500(request): | def swh_handle500(request): | ||||
""" | """ | ||||
Custom Django HTTP error 500 handler for swh-web. | Custom Django HTTP error 500 handler for swh-web. | ||||
""" | """ | ||||
error_description = 'An unexpected condition was encountered when '\ | error_description = 'An unexpected condition was encountered when '\ | ||||
'requesting resource %s.' %\ | 'requesting resource %s.' %\ | ||||
request.META['PATH_INFO'] | 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, html_response=True): | ||||
""" | """ | ||||
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. | ||||
""" | """ | ||||
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: | if html_response: | ||||
return _generate_error_page(request, error_code, error_description) | return _generate_error_page(request, error_code, error_description) | ||||
anlambert: Please move the call to `mark_safe` back into the `_generate_error_page` function. | |||||
else: | else: | ||||
return HttpResponse(error_description, content_type='text/plain', | return HttpResponse(error_description, content_type='text/plain', | ||||
status=error_code) | status=error_code) |
Please move the call to mark_safe back into the _generate_error_page function.