Conditions | 22 |
Total Lines | 88 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 506 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like ErrorReporterHandler._emit() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | from plugin.core.constants import PLUGIN_VERSION_BASE, PLUGIN_VERSION_BRANCH |
||
70 | def _emit(self, record, **kwargs): |
||
71 | data = { |
||
72 | 'user': {'id': self.client.name} |
||
73 | } |
||
74 | |||
75 | extra = getattr(record, 'data', None) |
||
76 | if not isinstance(extra, dict): |
||
77 | if extra: |
||
78 | extra = {'data': extra} |
||
79 | else: |
||
80 | extra = {} |
||
81 | |||
82 | for k, v in six.iteritems(vars(record)): |
||
83 | if k in RESERVED: |
||
84 | continue |
||
85 | if k.startswith('_'): |
||
86 | continue |
||
87 | if '.' not in k and k not in ('culprit', 'server_name'): |
||
88 | extra[k] = v |
||
89 | else: |
||
90 | data[k] = v |
||
91 | |||
92 | stack = getattr(record, 'stack', None) |
||
93 | if stack is True: |
||
94 | stack = iter_stack_frames() |
||
95 | |||
96 | if stack: |
||
97 | stack = self._get_targetted_stack(stack, record) |
||
98 | |||
99 | date = datetime.datetime.utcfromtimestamp(record.created) |
||
100 | event_type = 'raven.events.Message' |
||
101 | handler_kwargs = { |
||
102 | 'params': record.args, |
||
103 | } |
||
104 | try: |
||
105 | handler_kwargs['message'] = six.text_type(record.msg) |
||
106 | except UnicodeDecodeError: |
||
107 | # Handle binary strings where it should be unicode... |
||
108 | handler_kwargs['message'] = repr(record.msg)[1:-1] |
||
109 | |||
110 | try: |
||
111 | handler_kwargs['formatted'] = six.text_type(record.message) |
||
112 | except UnicodeDecodeError: |
||
113 | # Handle binary strings where it should be unicode... |
||
114 | handler_kwargs['formatted'] = repr(record.message)[1:-1] |
||
115 | |||
116 | # If there's no exception being processed, exc_info may be a 3-tuple of None |
||
117 | # http://docs.python.org/library/sys.html#sys.exc_info |
||
118 | exception_hash = None |
||
119 | |||
120 | if record.exc_info and all(record.exc_info): |
||
121 | # capture the standard message first so that we ensure |
||
122 | # the event is recorded as an exception, in addition to having our |
||
123 | # message interface attached |
||
124 | handler = self.client.get_handler(event_type) |
||
125 | data.update(handler.capture(**handler_kwargs)) |
||
126 | |||
127 | event_type = 'raven.events.Exception' |
||
128 | handler_kwargs = {'exc_info': record.exc_info} |
||
129 | |||
130 | # Calculate exception hash |
||
131 | exception_hash = ErrorHasher.hash(exc_info=record.exc_info) |
||
132 | |||
133 | # HACK: discover a culprit when we normally couldn't |
||
134 | elif not (data.get('stacktrace') or data.get('culprit')) and (record.name or record.funcName): |
||
135 | culprit = label_from_frame({'module': record.name, 'function': record.funcName}) |
||
136 | if culprit: |
||
137 | data['culprit'] = culprit |
||
138 | |||
139 | data['level'] = record.levelno |
||
140 | data['logger'] = record.name |
||
141 | |||
142 | # Store record `tags` in message |
||
143 | if hasattr(record, 'tags'): |
||
144 | kwargs['tags'] = record.tags |
||
145 | |||
146 | if exception_hash: |
||
147 | # Store `exception_hash` in message |
||
148 | if 'tags' not in kwargs: |
||
149 | kwargs['tags'] = {} |
||
150 | |||
151 | kwargs['tags']['exception.hash'] = exception_hash |
||
152 | |||
153 | kwargs.update(handler_kwargs) |
||
154 | |||
155 | return self.client.capture( |
||
156 | event_type, stack=stack, data=data, |
||
157 | extra=extra, date=date, **kwargs |
||
158 | ) |
||
167 |