Conditions | 28 |
Total Lines | 106 |
Lines | 0 |
Ratio | 0 % |
Tests | 1 |
CRAP Score | 774.7391 |
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 |
|
84 | 1 | def _emit(self, record, **kwargs): |
|
85 | data = { |
||
86 | 'user': {'id': self.client.name} |
||
87 | } |
||
88 | |||
89 | extra = getattr(record, 'data', None) |
||
90 | if not isinstance(extra, dict): |
||
91 | if extra: |
||
92 | extra = {'data': extra} |
||
93 | else: |
||
94 | extra = {} |
||
95 | |||
96 | for k, v in six.iteritems(vars(record)): |
||
97 | if k in RESERVED: |
||
98 | continue |
||
99 | if k.startswith('_'): |
||
100 | continue |
||
101 | if '.' not in k and k not in ('culprit', 'server_name'): |
||
102 | extra[k] = v |
||
103 | else: |
||
104 | data[k] = v |
||
105 | |||
106 | stack = getattr(record, 'stack', None) |
||
107 | if stack is True: |
||
108 | stack = iter_stack_frames() |
||
109 | |||
110 | if stack: |
||
111 | stack = self._get_targetted_stack(stack, record) |
||
112 | |||
113 | date = datetime.datetime.utcfromtimestamp(record.created) |
||
114 | event_type = 'raven.events.Message' |
||
115 | handler_kwargs = { |
||
116 | 'params': record.args, |
||
117 | } |
||
118 | try: |
||
119 | handler_kwargs['message'] = six.text_type(record.msg) |
||
120 | except UnicodeDecodeError: |
||
121 | # Handle binary strings where it should be unicode... |
||
122 | handler_kwargs['message'] = repr(record.msg)[1:-1] |
||
123 | |||
124 | try: |
||
125 | handler_kwargs['formatted'] = six.text_type(record.message) |
||
126 | except UnicodeDecodeError: |
||
127 | # Handle binary strings where it should be unicode... |
||
128 | handler_kwargs['formatted'] = repr(record.message)[1:-1] |
||
129 | |||
130 | # If there's no exception being processed, exc_info may be a 3-tuple of None |
||
131 | # http://docs.python.org/library/sys.html#sys.exc_info |
||
132 | try: |
||
133 | exc_info = self._exc_info(record) |
||
134 | except Exception, ex: |
||
135 | log.info('Unable to retrieve exception info - %s', ex, exc_info=True) |
||
136 | exc_info = None |
||
137 | |||
138 | exception_hash = None |
||
139 | |||
140 | if exc_info and len(exc_info) == 3 and all(exc_info): |
||
141 | message = handler_kwargs.get('formatted') |
||
142 | |||
143 | # Replace exception messages with more helpful details |
||
144 | if not record.exc_info and message and RE_TRACEBACK_HEADER.match(message): |
||
145 | # Generate new record title |
||
146 | handler_kwargs['formatted'] = '%s\n\n%s' % ( |
||
147 | self._generate_title(record, exc_info), |
||
148 | message |
||
149 | ) |
||
150 | elif not record.exc_info: |
||
151 | log.debug("Message %r doesn't match traceback header", message) |
||
152 | |||
153 | # capture the standard message first so that we ensure |
||
154 | # the event is recorded as an exception, in addition to having our |
||
155 | # message interface attached |
||
156 | handler = self.client.get_handler(event_type) |
||
157 | data.update(handler.capture(**handler_kwargs)) |
||
158 | |||
159 | event_type = 'raven.events.Exception' |
||
160 | handler_kwargs = {'exc_info': exc_info} |
||
161 | |||
162 | # Calculate exception hash |
||
163 | exception_hash = ErrorHasher.hash(exc_info=exc_info) |
||
164 | |||
165 | # HACK: discover a culprit when we normally couldn't |
||
166 | elif not (data.get('stacktrace') or data.get('culprit')) and (record.name or record.funcName): |
||
167 | culprit = label_from_frame({'module': record.name, 'function': record.funcName}) |
||
168 | if culprit: |
||
169 | data['culprit'] = culprit |
||
170 | |||
171 | data['level'] = record.levelno |
||
172 | data['logger'] = record.name |
||
173 | |||
174 | # Store record `tags` in message |
||
175 | if hasattr(record, 'tags'): |
||
176 | kwargs['tags'] = record.tags |
||
177 | |||
178 | if exception_hash: |
||
179 | # Store `exception_hash` in message |
||
180 | if 'tags' not in kwargs: |
||
181 | kwargs['tags'] = {} |
||
182 | |||
183 | kwargs['tags']['exception.hash'] = exception_hash |
||
184 | |||
185 | kwargs.update(handler_kwargs) |
||
186 | |||
187 | return self.client.capture( |
||
188 | event_type, stack=stack, data=data, |
||
189 | extra=extra, date=date, **kwargs |
||
190 | ) |
||
361 |