errors.py 1.46 KB
Newer Older
1
2
3
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
4
# This file is part of CERN Search.
Carina Antunes's avatar
Carina Antunes committed
5
# Copyright (C) 2018-2021 CERN.
6
#
7
# Citadel Search is free software; you can redistribute it and/or modify it
8
# under the terms of the MIT License; see LICENSE file for more details.
9
"""Custom errors."""
10

Carina Antunes's avatar
Carina Antunes committed
11
from invenio_rest.errors import RESTException, RESTValidationError
12
13
14
15
16
17


class InvalidRecordFormatError(RESTValidationError):
    """Invalid query syntax."""

    code = 400
Carina Antunes's avatar
Carina Antunes committed
18
    description = "Invalid query syntax."
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34


class SearchError(Exception):
    """Base class for Search errors."""

    def __init__(self, message):
        """Initialize exception."""
        self.message = message


class ObjectNotFoundError(SearchError):
    """Base class for Search errors."""

    def __str__(self):
        """Return description."""
        return f"{self.message} not found."
Carina Antunes's avatar
Carina Antunes committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63


class Error(object):
    """Represents a generic error.

    .. note:: This is not an actual exception.
    """

    def __init__(self, cause: str):
        """Init object.

        :param cause: The string error.
        """
        self.res = dict(cause=cause)

    def to_dict(self):
        """Convert to dictionary.

        :returns: A dictionary with field, message and, if initialized, the
            HTTP status code.
        """
        return self.res


class ConflictError(RESTException):
    """Conflict Error exception."""

    code = 409
    description = "An internal error occurred due to a conflict in the internal state."