-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathexceptions.py
113 lines (72 loc) · 3.16 KB
/
exceptions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"""Collection of useful http error for the Api"""
class JsonApiException(Exception):
"""Base exception class for unknown errors"""
title = 'Unknown error'
status = '500'
source = None
def __init__(self, detail, source=None, title=None, status=None, code=None, id_=None, links=None, meta=None):
"""Initialize a jsonapi exception
:param dict source: the source of the error
:param str detail: the detail of the error
"""
self.detail = detail
self.source = source
self.code = code
self.id = id_
self.links = links or {}
self.meta = meta or {}
if title is not None:
self.title = title
if status is not None:
self.status = status
def to_dict(self):
"""Return values of each fields of an jsonapi error"""
error_dict = {}
for field in ('status', 'source', 'title', 'detail', 'id', 'code', 'links', 'meta'):
if getattr(self, field, None):
error_dict.update({field: getattr(self, field)})
return error_dict
class BadRequest(JsonApiException):
"""BadRequest error"""
title = 'Bad request'
status = '400'
class InvalidField(BadRequest):
"""Error to warn that a field specified in fields querystring is not in the requested resource schema"""
title = "Invalid fields querystring parameter."
source = {'parameter': 'fields'}
class InvalidInclude(BadRequest):
"""Error to warn that a field specified in include querystring parameter is not a relationship of the requested
resource schema
"""
title = 'Invalid include querystring parameter.'
source = {'parameter': 'include'}
class InvalidFilters(BadRequest):
"""Error to warn that a specified filters in querystring parameter contains errors"""
title = 'Invalid filters querystring parameter.'
source = {'parameter': 'filters'}
class InvalidSort(BadRequest):
"""Error to warn that a field specified in sort querystring parameter is not in the requested resource schema"""
title = 'Invalid sort querystring parameter.'
source = {'parameter': 'sort'}
class ObjectNotFound(JsonApiException):
"""Error to warn that an object is not found in a database"""
title = 'Object not found'
status = '404'
class RelatedObjectNotFound(ObjectNotFound):
"""Error to warn that a related object is not found"""
title = 'Related object not found'
class RelationNotFound(JsonApiException):
"""Error to warn that a relationship is not found on a model"""
title = 'Relation not found'
class InvalidType(JsonApiException):
"""Error to warn that there is a conflit between resource types"""
title = 'Invalid type'
status = '409'
class AccessDenied(JsonApiException):
"""Throw this error when requested resource owner doesn't match the user of the ticket"""
title = 'Access denied'
status = '403'
class JsonApiPluginException(Exception):
"""Base class for all JsonApi in plugin-related errors."""
class PluginMethodNotImplementedError(JsonApiPluginException, NotImplementedError):
"""Raised when calling an unimplemented helper method in a plugin"""