Changeset View
Changeset View
Standalone View
Standalone View
swh/web/api/apiresponse.py
Show All 18 Lines | |||||
from rest_framework.utils.encoders import JSONEncoder | from rest_framework.utils.encoders import JSONEncoder | ||||
from swh.storage.exc import StorageAPIError, StorageDBError | from swh.storage.exc import StorageAPIError, StorageDBError | ||||
from swh.web.api import utils | from swh.web.api import utils | ||||
from swh.web.common.exc import BadInputExc, ForbiddenExc, LargePayloadExc, NotFoundExc | from swh.web.common.exc import BadInputExc, ForbiddenExc, LargePayloadExc, NotFoundExc | ||||
from swh.web.common.utils import gen_path_info, shorten_path | from swh.web.common.utils import gen_path_info, shorten_path | ||||
from swh.web.config import get_config | from swh.web.config import get_config | ||||
logger = logging.Logger(__name__) | logger = logging.getLogger("django") | ||||
def compute_link_header(rv: Dict[str, Any], options: Dict[str, Any]) -> Dict[str, Any]: | def compute_link_header(rv: Dict[str, Any], options: Dict[str, Any]) -> Dict[str, Any]: | ||||
"""Add Link header in returned value results. | """Add Link header in returned value results. | ||||
Args: | Args: | ||||
request: a DRF Request object | request: a DRF Request object | ||||
rv (dict): dictionary with keys: | rv (dict): dictionary with keys: | ||||
▲ Show 20 Lines • Show All 163 Lines • ▼ Show 20 Lines | error_data = { | ||||
"reason": str(exception), | "reason": str(exception), | ||||
} | } | ||||
if request.accepted_media_type == "text/html": | if request.accepted_media_type == "text/html": | ||||
error_data["reason"] = escape(error_data["reason"]) | 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() | ||||
logger.error(error_data["traceback"]) | logger.debug(error_data["traceback"]) | ||||
return make_api_response(request, error_data, doc_data, options=error_opts) | return make_api_response(request, error_data, doc_data, options=error_opts) | ||||
def error_response_handler( | def error_response_handler( | ||||
exc: Exception, context: Dict[str, Any] | exc: Exception, context: Dict[str, Any] | ||||
) -> Optional[HttpResponse]: | ) -> Optional[HttpResponse]: | ||||
"""Custom DRF exception handler used to generate API error responses. | """Custom DRF exception handler used to generate API error responses. | ||||
""" | """ | ||||
sentry_sdk.capture_exception(exc) | sentry_sdk.capture_exception(exc) | ||||
doc_data = getattr(exc, "doc_data", None) | doc_data = getattr(exc, "doc_data", None) | ||||
return error_response(context["request"], exc, doc_data) | return error_response(context["request"], exc, doc_data) |