Changeset View
Changeset View
Standalone View
Standalone View
swh/web/api/apiresponse.py
# Copyright (C) 2017-2018 The Software Heritage developers | # Copyright (C) 2017-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 json | import json | ||||
import traceback | import traceback | ||||
from django.utils.html import escape | |||||
from rest_framework.response import Response | from rest_framework.response import Response | ||||
from swh.storage.exc import StorageDBError, StorageAPIError | from swh.storage.exc import StorageDBError, StorageAPIError | ||||
from swh.web.api import utils | from swh.web.api import utils | ||||
from swh.web.common.exc import NotFoundExc, ForbiddenExc | from swh.web.common.exc import NotFoundExc, ForbiddenExc | ||||
from swh.web.common.utils import shorten_path, gen_path_info | from swh.web.common.utils import shorten_path, gen_path_info | ||||
from swh.web.config import get_config | from swh.web.config import get_config | ||||
▲ Show 20 Lines • Show All 153 Lines • ▼ Show 20 Lines | def error_response(request, error, doc_data): | ||||
elif isinstance(error, StorageDBError): | elif isinstance(error, StorageDBError): | ||||
error_code = 503 | error_code = 503 | ||||
elif isinstance(error, StorageAPIError): | elif isinstance(error, StorageAPIError): | ||||
error_code = 503 | error_code = 503 | ||||
error_opts = {'status': error_code} | error_opts = {'status': error_code} | ||||
error_data = { | error_data = { | ||||
'exception': error.__class__.__name__, | 'exception': error.__class__.__name__, | ||||
'reason': str(error), | 'reason': str(error), | ||||
anlambert: Here I would escape the error string only when the request media type is `text/html` as JSON… | |||||
Not Done Inline ActionsThis is why the tests are failing by the way: https://jenkins.softwareheritage.org/job/DWAPPS/job/tox/429/console anlambert: This is why the tests are failing by the way: https://jenkins.softwareheritage. | |||||
Not Done Inline ActionsYou need to remove the escaping here anlambert: You need to remove the escaping here | |||||
} | } | ||||
if request.accepted_media_type == 'text/html': | |||||
error_data['reason'] = escape(error_data['reason']) | |||||
if get_config()['debug']: | if get_config()['debug']: | ||||
error_data['traceback'] = traceback.format_exc() | error_data['traceback'] = traceback.format_exc() | ||||
return make_api_response(request, error_data, doc_data, | return make_api_response(request, error_data, doc_data, | ||||
options=error_opts) | options=error_opts) |
Here I would escape the error string only when the request media type is text/html as JSON error reponses will look strange otherwise.
You can use the following test to detect the media type: