-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathflask.py
More file actions
81 lines (63 loc) · 2.31 KB
/
flask.py
File metadata and controls
81 lines (63 loc) · 2.31 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
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
from ...core import Hawk
from typing import Union
from hawk_python_sdk.modules.flask.types import FlaskSettings, Addons
from hawk_python_sdk.errors import ModuleError
try:
from flask.signals import got_request_exception
from flask import Flask, request
except ImportError:
raise ModuleError("Flask is not installed")
# class for catching errors in flask app
class HawkFlask(Hawk):
params: FlaskSettings = {}
def init(self, settings: Union[str, FlaskSettings] = None) -> None:
self.params = self.get_params(settings)
got_request_exception.connect(self._handle_request_exception)
@staticmethod
def get_params(settings) -> Union[FlaskSettings, None]:
hawk_params = Hawk.get_params(settings)
if hawk_params is None:
return None
return {
**hawk_params,
'set_user': settings.get('set_user'),
}
def send(self, exception, context=None, user=None):
"""
Method for manually send error to Hawk
:param exception: exception
:param context: additional context to send with error
:param user: user information who faced with that event
"""
if (user is None) and (request):
user = self._set_user(request)
super().send(exception, context, user)
def _handle_request_exception(self, sender: Flask, exception):
"""
Catch, prepare and send error
:param sender: flask app
:param exception: exception
"""
user = self._set_user(request)
ctx = self.params.get('context', None)
self.send(exception, ctx, user)
def _set_addons(self) -> Union[Addons, None]:
"""
Set flask addons to send with error
"""
addons: Union[Addons, None] = None
if self.params.get('with_addons') == True:
headers = dict(request.headers)
cookies = dict(request.cookies)
addons = {
'flask': {
'url': request.url,
'method': request.method,
'headers': headers,
'cookies': cookies,
'params': request.args,
'form': request.form,
'json': request.json
}
}
return addons