| Conditions | 14 |
| Total Lines | 66 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 ErrorHandlingMiddleware.__call__() 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 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
| 36 | def __call__(self, environ, start_response): |
||
| 37 | try: |
||
| 38 | try: |
||
| 39 | return self.app(environ, start_response) |
||
| 40 | except NotFoundException: |
||
| 41 | raise exc.HTTPNotFound() |
||
| 42 | except Exception as e: |
||
| 43 | status = getattr(e, 'code', exc.HTTPInternalServerError.code) |
||
| 44 | |||
| 45 | if hasattr(e, 'detail') and not getattr(e, 'comment'): |
||
| 46 | setattr(e, 'comment', getattr(e, 'detail')) |
||
| 47 | |||
| 48 | if hasattr(e, 'body') and isinstance(getattr(e, 'body', None), dict): |
||
| 49 | body = getattr(e, 'body', None) |
||
| 50 | else: |
||
| 51 | body = {} |
||
| 52 | |||
| 53 | if isinstance(e, exc.HTTPException): |
||
| 54 | status_code = status |
||
| 55 | message = str(e) |
||
| 56 | elif isinstance(e, db_exceptions.StackStormDBObjectNotFoundError): |
||
| 57 | status_code = exc.HTTPNotFound.code |
||
| 58 | message = str(e) |
||
| 59 | elif isinstance(e, db_exceptions.StackStormDBObjectConflictError): |
||
| 60 | status_code = exc.HTTPConflict.code |
||
| 61 | message = str(e) |
||
| 62 | body['conflict-id'] = getattr(e, 'conflict_id', None) |
||
| 63 | elif isinstance(e, rbac_exceptions.AccessDeniedError): |
||
| 64 | status_code = exc.HTTPForbidden.code |
||
| 65 | message = str(e) |
||
| 66 | elif isinstance(e, (ValueValidationException, ValueError, ValidationError)): |
||
| 67 | status_code = exc.HTTPBadRequest.code |
||
| 68 | message = getattr(e, 'message', str(e)) |
||
| 69 | else: |
||
| 70 | status_code = exc.HTTPInternalServerError.code |
||
| 71 | message = 'Internal Server Error' |
||
| 72 | |||
| 73 | # Log the error |
||
| 74 | is_internal_server_error = status_code == exc.HTTPInternalServerError.code |
||
| 75 | error_msg = getattr(e, 'comment', str(e)) |
||
| 76 | extra = { |
||
| 77 | 'exception_class': e.__class__.__name__, |
||
| 78 | 'exception_message': str(e), |
||
| 79 | 'exception_data': e.__dict__ |
||
| 80 | } |
||
| 81 | |||
| 82 | if is_internal_server_error: |
||
| 83 | LOG.exception('API call failed: %s', error_msg, extra=extra) |
||
| 84 | LOG.exception(traceback.format_exc()) |
||
| 85 | else: |
||
| 86 | LOG.debug('API call failed: %s', error_msg, extra=extra) |
||
| 87 | |||
| 88 | if is_debugging_enabled(): |
||
| 89 | LOG.debug(traceback.format_exc()) |
||
| 90 | |||
| 91 | body['faultstring'] = message |
||
| 92 | |||
| 93 | response_body = json_encode(body) |
||
| 94 | headers = { |
||
| 95 | 'Content-Type': 'application/json', |
||
| 96 | 'Content-Length': str(len(response_body)) |
||
| 97 | } |
||
| 98 | |||
| 99 | resp = Response(response_body, status=status_code, headers=headers) |
||
| 100 | |||
| 101 | return resp(environ, start_response) |
||
|
|
|||
| 102 |