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 | ||||
""" | """ | ||||
import logging | import logging | ||||
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 131 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 raise_missing_slug_error() -> NoReturn: | |||||
"""Returns a missing slug header error dict | |||||
""" | |||||
raise DepositError( | |||||
BAD_REQUEST, | |||||
"Missing SLUG header", | |||||
verbose_description=( | |||||
"Provide in the SLUG header one identifier, for example the " | |||||
"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=None): | def __init__(self, key, summary, verbose_description=None): | ||||
self.key = key | self.key = key | ||||
self.summary = summary | self.summary = summary | ||||
Show All 33 Lines |