sentry-python icon indicating copy to clipboard operation
sentry-python copied to clipboard

ignore_errors parameter cannot ignore the specified exceptions.

Open yuqiuwen opened this issue 1 year ago • 10 comments

Environment

SaaS (https://sentry.io/)

Steps to Reproduce

sentry_sdk.init(
        dsn=os.getenv("SENTRY_DSN"),
        integrations=[FlaskIntegration()],
        environment=env,
        ignore_errors=[AuthException]
)

AuthException inherits from Exception

Expected Result

ignore AuthException

Actual Result

AuthException is still captured

Product Area

Unknown

Link

No response

DSN

No response

Version

No response

yuqiuwen avatar Jan 24 '24 02:01 yuqiuwen

Assigning to @getsentry/support for routing ⏲️

getsantry[bot] avatar Jan 24 '24 02:01 getsantry[bot]

Routing to @getsentry/product-owners-settings-integrations for triage ⏲️

getsantry[bot] avatar Jan 24 '24 17:01 getsantry[bot]

Routing to @getsentry/product-owners-settings-auth for triage ⏲️

getsantry[bot] avatar Jan 24 '24 17:01 getsantry[bot]

@ReneGreen27 can you please transfer this issue to the SDK specific repository. This is not an Auth issue

Dhrumil-Sentry avatar Jan 24 '24 17:01 Dhrumil-Sentry

@ReneGreen27 can you please transfer this issue to the SDK specific repository. This is not an Auth issue

maybe you don't understand my mind, I mean that ignore_errors parameter does not work, instead of Auth issue

yuqiuwen avatar Jan 25 '24 06:01 yuqiuwen

Hey @yuqiuwen, thanks for writing in.

I can't reproduce the issue. Here's my small Flask app:

import sentry_sdk
from flask import Flask

class AuthException(Exception):
    pass

sentry_sdk.init(
    dsn=<DSN>,
    debug=True,
    ignore_errors=[AuthException],
)

app = Flask(__name__)

@app.route("/autherror")
def auth_error():
    raise AuthException('oh no')
    return "ok"

Requesting /autherror results in an Internal Server Error, as expected, but the AuthException is not sent to Sentry.

Could you provide a minimal reproducible example to help us reproduce?

sentrivana avatar Jan 26 '24 10:01 sentrivana

Hey @yuqiuwen, thanks for writing in.

I can't reproduce the issue. Here's my small Flask app:

import sentry_sdk
from flask import Flask

class AuthException(Exception):
    pass

sentry_sdk.init(
    dsn=<DSN>,
    debug=True,
    ignore_errors=[AuthException],
)

app = Flask(__name__)

@app.route("/autherror")
def auth_error():
    raise AuthException('oh no')
    return "ok"

Requesting /autherror results in an Internal Server Error, as expected, but the AuthException is not sent to Sentry.

Could you provide a minimal reproducible example to help us reproduce?

this is my code:

class AuthException(Exception):
    def __init__(self, message="", code: Enum = AppCode.AUTH_ERROR, errmsg=None):
        super().__init__(message)
        self.code = code.value
        self.errmsg = errmsg

however, it worked for me using before_send

def before_send(event, hint):
    if "exc_info" in hint:
        exc_type, exc_value, tb = hint["exc_info"]
        if isinstance(exc_value, (AuthException,)):
            return None
        return event

yuqiuwen avatar Jan 27 '24 12:01 yuqiuwen

@yuqiuwen before_send is another way to do it, great that that works for you. I'd still like to understand why ignore_errors doesn't though, but for that I'd need a full example of a small app where ignore_errors doesn't work. So far with my small app as outlined above (with your AuthException) it all works as expected.

sentrivana avatar Jan 29 '24 09:01 sentrivana

@yuqiuwen before_send is another way to do it, great that that works for you. I'd still like to understand why ignore_errors doesn't though, but for that I'd need a full example of a small app where ignore_errors doesn't work. So far with my small app as outlined above (with your AuthException) it all works as expected.

Maybe I see , because of logging the error, is right? if i don't log the error, it works well.

@app.errorhandler(AuthException)
def handle_auth_error(e):
    logger.error(traceback.format_exc())
    return make_response(str(e), code=e.code, errmsg=e.errmsg)

yuqiuwen avatar Jan 29 '24 10:01 yuqiuwen

Maybe I see , because of logging the error, is right? if i don't log the error, it works well.

@app.errorhandler(AuthException)
def handle_auth_error(e):
    logger.error(traceback.format_exc())
    return make_response(str(e), code=e.code, errmsg=e.errmsg)

Right, it's our logging integration capturing it. Thanks for digging into it!

I think this could be solved by improving our ignored error detection to do something similar like you did in your before_send.

I will put this in our backlog.

sentrivana avatar Jan 29 '24 11:01 sentrivana

Maybe I see , because of logging the error, is right? if i don't log the error, it works well.

@app.errorhandler(AuthException)
def handle_auth_error(e):
    logger.error(traceback.format_exc())
    return make_response(str(e), code=e.code, errmsg=e.errmsg)

Right, it's our logging integration capturing it. Thanks for digging into it!

I think this could be solved by improving our ignored error detection to do something similar like you did in your before_send.

I will put this in our backlog.

here's another question. Why is the log output incomplete? image

yuqiuwen avatar Apr 08 '24 01:04 yuqiuwen

here's another question. Why is the log output incomplete?

@yuqiuwen Where are you seeing this? Can you please send the URL of the page in Sentry?

szokeasaurusrex avatar Apr 08 '24 12:04 szokeasaurusrex

here's another question. Why is the log output incomplete?

@yuqiuwen Where are you seeing this? Can you please send the URL of the page in Sentry?

This is the output of error.log, the part after the red line is truncated in the sentry image

yuqiuwen avatar Apr 09 '24 01:04 yuqiuwen

It probably got truncated by some part of the system because it was too long. Not sure off the top of my head if this is the SDK or the server doing this. Potentially might also just be a display thing. If you check the event JSON in Sentry (the button with the curly brackets), do you see the whole message?

sentrivana avatar Apr 09 '24 12:04 sentrivana

It probably got truncated by some part of the system because it was too long. Not sure off the top of my head if this is the SDK or the server doing this. Potentially might also just be a display thing. If you check the event JSON in Sentry (the button with the curly brackets), do you see the whole message?

click the button still only shows part of the log image

yuqiuwen avatar Apr 10 '24 02:04 yuqiuwen

@yuqiuwen that is just our server side truncation because the message is too long. The ideal way is to not let this captured by logging but as a normal exception so that this goes into the stacktrace and not serialized into the message string.

You can also use logging.error('bla', exc_info=True) and the stacktrace will be added to the sentry event.

sl0thentr0py avatar Apr 10 '24 13:04 sl0thentr0py