Skip to content
This repository was archived by the owner on Dec 16, 2025. It is now read-only.

Commit f53f0c2

Browse files
committed
enhance error handling
1 parent 6ab02ab commit f53f0c2

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

flask_rest_jsonapi/decorators.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ def wrapper(*args, **kwargs):
2222
error = jsonify(jsonapi_errors([{'source': '',
2323
'detail': "Content-Type header must be application/vnd.api+json",
2424
'title': 'InvalidRequestHeader',
25-
'status': 415}]))
25+
'status': '415'}]))
2626
return make_response(error, 415, {'Content-Type': 'application/vnd.api+json'})
2727
if request.headers.get('Accept') and 'application/vnd.api+json' not in request.accept_mimetypes:
2828
error = jsonify(jsonapi_errors([{'source': '',
2929
'detail': "Accept header must be application/vnd.api+json",
3030
'title': 'InvalidRequestHeader',
31-
'status': 406}]))
31+
'status': '406'}]))
3232
return make_response(error, 406, {'Content-Type': 'application/vnd.api+json'})
3333
return func(*args, **kwargs)
3434
return wrapper

flask_rest_jsonapi/exceptions.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@ class JsonApiException(Exception):
77
"""Base exception class for unknown errors"""
88

99
title = 'Unknown error'
10-
status = 500
10+
status = '500'
1111

12-
def __init__(self, source, detail, title=None, status=None):
12+
def __init__(self, source, detail, title=None, status=None, code=None, id_=None, links=None, meta=None):
1313
"""Initialize a jsonapi exception
1414
1515
:param dict source: the source of the error
1616
:param str detail: the detail of the error
1717
"""
1818
self.source = source
1919
self.detail = detail
20+
self.code = code
21+
self.id = id_
22+
self.links = links or {}
23+
self.meta = meta or {}
2024
if title is not None:
2125
self.title = title
2226
if status is not None:
@@ -27,14 +31,18 @@ def to_dict(self):
2731
return {'status': self.status,
2832
'source': self.source,
2933
'title': self.title,
30-
'detail': self.detail}
34+
'detail': self.detail,
35+
'id': self.id,
36+
'code': self.code,
37+
'links': self.links,
38+
'meta': self.meta}
3139

3240

3341
class BadRequest(JsonApiException):
3442
"""BadRequest error"""
3543

36-
title = "Bad request"
37-
status = 400
44+
title = 'Bad request'
45+
status = '400'
3846

3947

4048
class InvalidField(BadRequest):
@@ -56,7 +64,7 @@ class InvalidInclude(BadRequest):
5664
resource schema
5765
"""
5866

59-
title = "Invalid include querystring parameter."
67+
title = 'Invalid include querystring parameter.'
6068

6169
def __init__(self, detail):
6270
"""Initialize InvalidInclude error instance
@@ -70,7 +78,7 @@ def __init__(self, detail):
7078
class InvalidFilters(BadRequest):
7179
"""Error to warn that a specified filters in querystring parameter contains errors"""
7280

73-
title = "Invalid filters querystring parameter."
81+
title = 'Invalid filters querystring parameter.'
7482

7583
def __init__(self, detail):
7684
"""Initialize InvalidField error instance
@@ -84,7 +92,7 @@ def __init__(self, detail):
8492
class InvalidSort(BadRequest):
8593
"""Error to warn that a field specified in sort querystring parameter is not in the requested resource schema"""
8694

87-
title = "Invalid sort querystring parameter."
95+
title = 'Invalid sort querystring parameter.'
8896

8997
def __init__(self, detail):
9098
"""Initialize InvalidField error instance
@@ -98,24 +106,24 @@ def __init__(self, detail):
98106
class ObjectNotFound(JsonApiException):
99107
"""Error to warn that an object is not found in a database"""
100108

101-
title = "Object not found"
102-
status = 404
109+
title = 'Object not found'
110+
status = '404'
103111

104112

105113
class RelatedObjectNotFound(ObjectNotFound):
106114
"""Error to warn that a related object is not found"""
107115

108-
title = "Related object not found"
116+
title = 'Related object not found'
109117

110118

111119
class RelationNotFound(JsonApiException):
112120
"""Error to warn that a relationship is not found on a model"""
113121

114-
title = "Relation not found"
122+
title = 'Relation not found'
115123

116124

117125
class InvalidType(JsonApiException):
118126
"""Error to warn that there is a conflit between resource types"""
119127

120-
title = "Invalid type"
121-
status = 409
128+
title = 'Invalid type'
129+
status = '409'

flask_rest_jsonapi/resource.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,14 @@ def dispatch_request(self, *args, **kwargs):
7575
except Exception as e:
7676
if current_app.config['DEBUG'] is True:
7777
raise e
78-
exc = JsonApiException('', str(e))
78+
exc = JsonApiException(getattr(e, 'source', ''),
79+
getattr(e, 'detail', str(e)),
80+
title=getattr(e, 'title', None),
81+
status=getattr(e, 'status', None),
82+
code=getattr(e, 'code', None),
83+
id_=getattr(e, 'id', None),
84+
links=getattr(e, 'links', None),
85+
meta=getattr(e, 'meta', None))
7986
return make_response(jsonify(jsonapi_errors([exc.to_dict()])),
8087
exc.status,
8188
headers)

0 commit comments

Comments
 (0)