Total Complexity | 11 |
Total Lines | 50 |
Duplicated Lines | 0 % |
Coverage | 0% |
Changes | 0 |
1 | from raven.processors import Processor |
||
8 | class RelativePathProcessor(Processor): |
||
9 | separator = 'plug-ins' |
||
10 | |||
11 | def process(self, data, **kwargs): |
||
12 | data = super(RelativePathProcessor, self).process(data, **kwargs) |
||
13 | |||
14 | try: |
||
15 | extra = data.get('extra', {}) |
||
16 | |||
17 | self.to_relative(extra, 'pathname') |
||
18 | self.to_relative(extra.get('sys.argv', [])) |
||
19 | except Exception: |
||
|
|||
20 | log.warn('Exception raised in RelativePathProcessor.process()', exc_info=True) |
||
21 | |||
22 | return data |
||
23 | |||
24 | def filter_stacktrace(self, data, **kwargs): |
||
25 | for frame in data.get('frames', []): |
||
26 | try: |
||
27 | self.to_relative(frame, 'abs_path') |
||
28 | self.to_relative(frame, 'filename') |
||
29 | except Exception: |
||
30 | log.warn('Exception raised in RelativePathProcessor.filter_stacktrace()', exc_info=True) |
||
31 | |||
32 | def to_relative(self, d, key=None): |
||
33 | if type(d) is list: |
||
34 | if key is not None: |
||
35 | value = d[key] |
||
36 | else: |
||
37 | # Run `to_relative` on each item |
||
38 | for x in range(len(d)): |
||
39 | self.to_relative(d, x) |
||
40 | |||
41 | return True |
||
42 | else: |
||
43 | value = d.get(key) |
||
44 | |||
45 | if not value: |
||
46 | return False |
||
47 | |||
48 | # Find `separator` position |
||
49 | pos = os.path.normcase(value).find(self.separator) |
||
50 | |||
51 | if pos == -1: |
||
52 | return False |
||
53 | |||
54 | # Update `d[key]` with relative path |
||
55 | d[key] = value[pos:] |
||
56 | |||
57 | return True |
||
58 |
Generally, you would want to handle very specific errors in the exception handler. This ensure that you do not hide other types of errors which should be fixed.
So, unless you specifically plan to handle any error, consider adding a more specific exception.