Skip to content

Python agent errors on using capture exception in decorator function #599

Closed
@codeninja55

Description

@codeninja55

Describe the bug: ...
When trying to capture an exception in a decorator function, APM produces the following error.

File "/opt/conda/lib/python3.7/site-packages/flask_restful/__init__.py", line 573, in dispatch_request
    resp = meth(*args, **kwargs)
  File "/usr/src/app/sim_api/middleware.py", line 343, in decorated_func
    apm.capture_exception()
  File "/opt/conda/lib/python3.7/site-packages/elasticapm/contrib/flask/__init__.py", line 214, in capture_exception
    return self.client.capture_exception(*args, **kwargs)
  File "/opt/conda/lib/python3.7/site-packages/elasticapm/base.py", line 224, in capture_exception
    return self.capture("Exception", exc_info=exc_info, handled=handled, **kwargs)
  File "/opt/conda/lib/python3.7/site-packages/elasticapm/base.py", line 194, in capture
    event_type, date=date, context=context, custom=custom, stack=stack, handled=handled, **kwargs
  File "/opt/conda/lib/python3.7/site-packages/elasticapm/base.py", line 377, in _build_msg_for_logging
    result = handler.capture(self, **kwargs)
  File "/opt/conda/lib/python3.7/site-packages/elasticapm/events.py", line 119, in capture
    exc_type = exc_type.__name__
AttributeError: 'NoneType' object has no attribute '__name__'

To Reproduce

Take this function:

def auth_restful(f)
    @wraps(f)
    def decorated_func(*args, **kwargs):
        resp = User.decode_auth_token(auth_token=auth_token)
       
        if not user.is_admin:
            response['message'] = 'Not authorized.'
           # Error occurs HERE
            apm.capture_exception()
            return response, 403
        return f(user, *args, **kwargs)
    return decorated_func

The Flask-RESTful library and code to do this is as follows:

class UserList(Resource):
    """Return all users (admin only)"""

    method_decorators = {'get': [auth_restful]}

    def get(self, _) -> Tuple[dict, int]:
        """Get all users only available to admins."""
        user_list = User.as_dict
        response = {'status': 'success', 'data': {'users': user_list}}
        return response, 200

Expected behavior: ...

Environment (please complete the following information)

  • OS: [e.g. Linux]: Docker base continuumio/miniconda3:4.6.14
  • Python version: 3.7.3
  • Framework and version [e.g. Django 2.1]: Flask-RESTful
  • APM Server version: 5.1.2
  • Agent version: 5.1.2

Additional context

Add any other context about the problem here.

  • requirements.txt:

    Click to expand
    Flask==1.0.2
    elastic-apm[flask]==5.1.2
    fluent-logger==0.9.3
    Flask-RESTful==0.3.7
    Flask-Testing==0.7.1
    

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions