-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathutils.py
More file actions
75 lines (61 loc) · 2.38 KB
/
utils.py
File metadata and controls
75 lines (61 loc) · 2.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# -*- coding: utf-8 -*-
# Utils modules for flask plugin
#
# :copyright: 2020 Sonu Kumar
# :license: BSD-3-Clause
#
from error_tracker import ContextBuilderMixin, ViewPermissionMixin
class DefaultFlaskContextBuilder(ContextBuilderMixin):
"""
Default request builder, this records, form data, header and URL parameters and mask them if necessary
"""
def get_context(self, request, masking=None, additional_context=None):
request_data = dict()
if additional_context is not None and len(additional_context) != 0:
request_data['context'] = additional_context
if request is not None:
form = dict(request.form)
headers = dict(request.headers)
if masking:
for key in form:
masked, value = masking(key)
if masked:
form[key] = value
for key in headers:
masked, value = masking(key)
if masked:
headers[key] = value
request_data.update({
'headers': headers,
'args': dict(request.args),
'form': form
})
return str(request_data)
class DefaultFlaskViewPermission(ViewPermissionMixin):
def __call__(self, request):
return False
class configure_scope(object):
"""
Use this class to work with context manager where more context can be added on the fly
usage
with configure_scope() as scope:
scope.set_extra("id", 1234)
"""
def __init__(self, error_manager, context=None, handle_exception=True):
"""
Initialize class with error_manager instance.
:param error_manager: AppErrorTracker instance
:param handle_exception: whether raised exception is ignored or not, post exception capture
:param context: initial context detail, dictionary of key value pairs
"""
self.context = context or {}
self.error_manager = error_manager
self.handle_exception = handle_exception
def set_extra(self, key, value):
self.context[key] = value
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if exc_type is not None:
self.error_manager.capture_exception(additional_context=self.context)
return self.handle_exception