Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/errors.py
# Copyright (C) 2017-2019 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 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" | ||||
ERROR_CONTENT = "error-content" | ERROR_CONTENT = "error-content" | ||||
▲ Show 20 Lines • Show All 127 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]: | |||||
"""Returns a missing slug header error dict | |||||
""" | |||||
return make_error_dict( | |||||
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 BadRequestError(ValueError): | class BadRequestError(ValueError): | ||||
"""Represents a bad input from the deposit client | """Represents a bad input from the deposit client | ||||
""" | """ | ||||
def __init__(self, summary, verbose_description): | def __init__(self, summary, verbose_description): | ||||
self.key = BAD_REQUEST | self.key = BAD_REQUEST | ||||
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) |