Changeset View
Changeset View
Standalone View
Standalone View
swh/web/api/apiresponse.py
# Copyright (C) 2017-2019 The Software Heritage developers | # Copyright (C) 2017-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 json | import json | ||||
import traceback | import traceback | ||||
from django.utils.html import escape | 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, BadInputExc | from swh.web.common.exc import (NotFoundExc, ForbiddenExc, | ||||
BadInputExc, InputSizeExc) | |||||
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 | ||||
def compute_link_header(rv, options): | def compute_link_header(rv, options): | ||||
"""Add Link header in returned value results. | """Add Link header in returned value results. | ||||
Args: | Args: | ||||
▲ Show 20 Lines • Show All 135 Lines • ▼ Show 20 Lines | def error_response(request, error, doc_data): | ||||
""" | """ | ||||
error_code = 500 | error_code = 500 | ||||
if isinstance(error, BadInputExc): | if isinstance(error, BadInputExc): | ||||
error_code = 400 | error_code = 400 | ||||
elif isinstance(error, NotFoundExc): | elif isinstance(error, NotFoundExc): | ||||
error_code = 404 | error_code = 404 | ||||
elif isinstance(error, ForbiddenExc): | elif isinstance(error, ForbiddenExc): | ||||
error_code = 403 | error_code = 403 | ||||
elif isinstance(error, InputSizeExc): | |||||
error_code = 413 | |||||
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__, | ||||
Show All 11 Lines |