# Flask error handling removes error transactions from APM

**URL:** <https://discuss.elastic.co/t/flask-error-handling-removes-error-transactions-from-apm/281725>\
**Category:** APM\
**Tags:** elastic-stack-monitoring, python\
**Created:** [August 17, 2021, 4:54pm UTC](https://discuss.elastic.co/t/flask-error-handling-removes-error-transactions-from-apm/281725 "2021-08-17T16:54:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidshaw2018](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davidshaw2018/32/93295_2.png) [@davidshaw2018](https://discuss.elastic.co/u/davidshaw2018)\
**Post date:** [August 17, 2021, 4:54pm UTC](https://discuss.elastic.co/t/flask-error-handling-removes-error-transactions-from-apm/281725/1 "2021-08-17T16:54:24Z")

</div>

**Kibana version** : 7.11.1

**Elasticsearch version** : 7.11.1

**APM Server version** : 7.11.0

**APM Agent language and version** : Python 6.3.3

**Description of the problem including expected versus actual behavior. Please include screenshots (if relevant)**:

Hi, I'm running a Python Flask application and connected it to APM. However, I've noticed that when I add an error handler to my application as below, APM will see the error without providing a stack trace.

```auto
    @app.errorhandler(Exception)
    def handle_anything(error):
        try:
            status_code = error.status_code
        except AttributeError:
            status_code = 500
        response_dict = {"type": str(type(error)), "exception_message": str(error)}
        app.logger.exception(response_dict, status_code=status_code)
        response = jsonify(error=response_dict)
        response.content_type = "application/json"
        response.status_code = status_code
        return response

```

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/a/2/a2baca8ffbe67b40bdda5b55f2150dfae3694b95.png)

If I remove this error handling, I get the expected output.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/9/8/989f05dcf1d71a8615a8ee416a93a100adf73e37.png)

My question is can I add some more sophisticated error handling in a Flask application while still ensuring error traces are captured by the APM agent? Or explicitly ship error traces to APM server somehow?

---

<div class="post-metadata">

**Author:** ![basepi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/basepi/32/53056_2.png) [@basepi](https://discuss.elastic.co/u/basepi)\
**Post date:** [August 17, 2021, 5:09pm UTC](https://discuss.elastic.co/t/flask-error-handling-removes-error-transactions-from-apm/281725/2 "2021-08-17T17:09:08Z")

</div>

I have to admit I'm a little surprised that it's reporting it as an error but not collecting the trace. I think this might be a bug. Would you mind reporting it [here](https://github.com/elastic/apm-agent-python/issues/new?assignees=&labels=&template=Bug_report.md) so we don't lose track of it? I'll try to find some time to reproduce and look into it.

---

<div class="post-metadata">

**Author:** ![davidshaw2018](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davidshaw2018/32/93295_2.png) [@davidshaw2018](https://discuss.elastic.co/u/davidshaw2018)\
**Post date:** [August 17, 2021, 5:44pm UTC](https://discuss.elastic.co/t/flask-error-handling-removes-error-transactions-from-apm/281725/3 "2021-08-17T17:44:13Z")

</div>

Thanks! I will do so.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 7, 2021, 1:44pm UTC](https://discuss.elastic.co/t/flask-error-handling-removes-error-transactions-from-apm/281725/4 "2021-09-07T13:44:54Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
