This repository was archived by the owner on Nov 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_error_template.py
More file actions
52 lines (39 loc) · 1.38 KB
/
_error_template.py
File metadata and controls
52 lines (39 loc) · 1.38 KB
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
from flask import Response
from flask import jsonify
from flask_utils.errors.base_class import _BaseFlaskException
def _generate_error_json(error: _BaseFlaskException, status_code: int) -> Response:
"""
This function is used to generate a json of the error passed
:param error: The error containing the message and solution
:type error: _BaseFlaskException
:param status_code: The status code of the error.
:type status_code: int
:return: Returns a json containing all the info
:rtype: flask.Response
:Example:
.. code-block:: python
from flask_utils.errors import _BaseFlaskException
from flask_utils.errors._error_template import _generate_error_json
class MyError(_BaseFlaskException):
self.name = "MyError"
self.msg = msg
self.solution = solution
self.status_code = 666
error = MyError("This is an error", "This is the solution")
json = _generate_error_json(error, 666)
.. versionadded:: 0.1.0
"""
success = False
json = {
"success": success,
"error": {
"type": error.__class__.__name__,
"name": error.name,
"message": error.msg,
"solution": error.solution,
},
"code": status_code,
}
resp: Response = jsonify(json)
resp.status_code = status_code
return resp