Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/errors.py
# Copyright (C) 2017-2020 The Software Heritage developers | # Copyright (C) 2017-2020 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 General Public License version 3, or any later version | # License: GNU 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 | ||||
"""Module in charge of providing the standard sword errors | """Module in charge of providing the standard sword errors | ||||
""" | """ | ||||
from typing import Any, Dict | from typing import NoReturn | ||||
from django.shortcuts import render | from django.shortcuts import render | ||||
from rest_framework import status | from rest_framework import status | ||||
FORBIDDEN = "forbidden" | FORBIDDEN = "forbidden" | ||||
UNAUTHORIZED = "unauthorized" | UNAUTHORIZED = "unauthorized" | ||||
NOT_FOUND = "unknown" | NOT_FOUND = "unknown" | ||||
BAD_REQUEST = "bad-request" | BAD_REQUEST = "bad-request" | ||||
▲ Show 20 Lines • Show All 128 Lines • ▼ Show 20 Lines | Returns: | ||||
Dictionary with key 'error' detailing the 'status' and | Dictionary with key 'error' detailing the 'status' and | ||||
associated 'message' | associated 'message' | ||||
""" | """ | ||||
error = make_error_dict(key, summary, verbose_description) | error = make_error_dict(key, summary, verbose_description) | ||||
return make_error_response_from_dict(req, error["error"]) | return make_error_response_from_dict(req, error["error"]) | ||||
def make_missing_slug_error() -> Dict[str, Any]: | def raise_missing_slug_error() -> NoReturn: | ||||
"""Returns a missing slug header error dict | """Returns a missing slug header error dict | ||||
""" | """ | ||||
return make_error_dict( | raise DepositError( | ||||
BAD_REQUEST, | BAD_REQUEST, | ||||
"Missing SLUG header", | "Missing SLUG header", | ||||
verbose_description=( | verbose_description=( | ||||
"Provide in the SLUG header one identifier, for example the " | "Provide in the SLUG header one identifier, for example the " | ||||
"url pointing to the resource you are depositing." | "url pointing to the resource you are depositing." | ||||
), | ), | ||||
) | ) | ||||
class DepositError(ValueError): | class DepositError(ValueError): | ||||
"""Represents an error that should be reported to the client | """Represents an error that should be reported to the client | ||||
""" | """ | ||||
def __init__(self, key, summary, verbose_description): | def __init__(self, key, summary, verbose_description=None): | ||||
self.key = key | self.key = key | ||||
self.summary = summary | self.summary = summary | ||||
self.verbose_description = verbose_description | self.verbose_description = verbose_description | ||||
def to_dict(self): | def to_dict(self): | ||||
return make_error_dict(self.key, self.summary, self.verbose_description) | return make_error_dict(self.key, self.summary, self.verbose_description) | ||||
class DepositErrorMiddleware: | |||||
"""A Django middleware that catches DepositError and returns a proper | |||||
error response.""" | |||||
# __init__ and __call__ are boilerplate to make a pass-through Django | |||||
# middleware | |||||
def __init__(self, get_response): | |||||
self.get_response = get_response | |||||
def __call__(self, request): | |||||
response = self.get_response(request) | |||||
ardumontUnsubmitted Not Done Inline Actionsardumont: ```
return self.get_response(request)
``` | |||||
return response | |||||
def process_exception(self, request, exception): | |||||
if isinstance(exception, DepositError): | |||||
Not Done Inline ActionsIf we add a logging instruction on the current error here, we could close T2626 at the same time (or in another diff?) ardumont: If we add a logging instruction on the current error here, we could close T2626 at the same… | |||||
return make_error_response_from_dict(request, exception.to_dict()["error"]) | |||||
else: | |||||
return None |