Conditions | 22 |
Total Lines | 98 |
Lines | 0 |
Ratio | 0 % |
Tests | 1 |
CRAP Score | 478.6534 |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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 | 1 | from plugin.core.constants import PLUGIN_VERSION_BASE, PLUGIN_VERSION_BRANCH |
|
103 | 1 | def _emit(self, record, **kwargs): |
|
104 | data, extra = extract_extra(record) |
||
105 | |||
106 | # Use client name as default user id |
||
107 | data.setdefault('user', {'id': self.client.name}) |
||
108 | |||
109 | # Retrieve stack |
||
110 | stack = getattr(record, 'stack', None) |
||
111 | if stack is True: |
||
112 | stack = iter_stack_frames() |
||
113 | |||
114 | if stack: |
||
115 | stack = self._get_targetted_stack(stack, record) |
||
116 | |||
117 | # Build message |
||
118 | date = datetime.datetime.utcfromtimestamp(record.created) |
||
119 | event_type = 'raven.events.Message' |
||
120 | handler_kwargs = { |
||
121 | 'params': record.args, |
||
122 | } |
||
123 | |||
124 | try: |
||
125 | handler_kwargs['message'] = text_type(record.msg) |
||
126 | except UnicodeDecodeError: |
||
127 | # Handle binary strings where it should be unicode... |
||
128 | handler_kwargs['message'] = repr(record.msg)[1:-1] |
||
129 | |||
130 | try: |
||
131 | handler_kwargs['formatted'] = text_type(record.message) |
||
132 | except UnicodeDecodeError: |
||
133 | # Handle binary strings where it should be unicode... |
||
134 | handler_kwargs['formatted'] = repr(record.message)[1:-1] |
||
135 | |||
136 | # Retrieve exception information from record |
||
137 | try: |
||
138 | exc_info = self._exc_info(record) |
||
139 | except Exception as ex: |
||
140 | log.info('Unable to retrieve exception info - %s', ex, exc_info=True) |
||
141 | exc_info = None |
||
142 | |||
143 | # Parse exception information |
||
144 | exception_hash = None |
||
145 | |||
146 | # If there's no exception being processed, exc_info may be a 3-tuple of None |
||
147 | # http://docs.python.org/library/sys.html#sys.exc_info |
||
148 | if exc_info and len(exc_info) == 3 and all(exc_info): |
||
149 | message = handler_kwargs.get('formatted') |
||
150 | |||
151 | # Replace exception messages with more helpful details |
||
152 | if not record.exc_info and message and RE_TRACEBACK_HEADER.match(message): |
||
153 | # Generate new record title |
||
154 | handler_kwargs['formatted'] = '%s\n\n%s' % ( |
||
155 | self._generate_title(record, exc_info), |
||
156 | message |
||
157 | ) |
||
158 | elif not record.exc_info: |
||
159 | log.debug("Message %r doesn't match traceback header", message) |
||
160 | |||
161 | # capture the standard message first so that we ensure |
||
162 | # the event is recorded as an exception, in addition to having our |
||
163 | # message interface attached |
||
164 | handler = self.client.get_handler(event_type) |
||
165 | data.update(handler.capture(**handler_kwargs)) |
||
166 | |||
167 | event_type = 'raven.events.Exception' |
||
168 | handler_kwargs = {'exc_info': exc_info} |
||
169 | |||
170 | # Calculate exception hash |
||
171 | exception_hash = ErrorHasher.hash(exc_info=exc_info) |
||
172 | |||
173 | # HACK: discover a culprit when we normally couldn't |
||
174 | elif not (data.get('stacktrace') or data.get('culprit')) and (record.name or record.funcName): |
||
175 | culprit = self._label_from_frame({'module': record.name, 'function': record.funcName}) |
||
176 | |||
177 | if culprit: |
||
178 | data['culprit'] = culprit |
||
179 | |||
180 | data['level'] = record.levelno |
||
181 | data['logger'] = record.name |
||
182 | |||
183 | # Store record `tags` in message |
||
184 | if hasattr(record, 'tags'): |
||
185 | kwargs['tags'] = record.tags |
||
186 | elif self.tags: |
||
187 | kwargs['tags'] = self.tags |
||
188 | |||
189 | # Store `exception_hash` in message (if defined) |
||
190 | if exception_hash: |
||
191 | if 'tags' not in kwargs: |
||
192 | kwargs['tags'] = {} |
||
193 | |||
194 | kwargs['tags']['exception.hash'] = exception_hash |
||
195 | |||
196 | kwargs.update(handler_kwargs) |
||
197 | |||
198 | return self.client.capture( |
||
199 | event_type, stack=stack, data=data, |
||
200 | extra=extra, date=date, **kwargs |
||
201 | ) |
||
384 |