-
Notifications
You must be signed in to change notification settings - Fork 535
Sentry doesn't capture aiohttp exception? #642
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I'm not sure if it helps at all. But maybe it gives some ideas for people who will work on this issue. It is possible to get an error if reraise exception again: async def errors_handler(request, handler):
try:
response = await handler(request)
except web.HTTPException as e:
capture_exception(e)
raise But this is making impossible to capture error pages and publish custom error page. |
But, if you use this tricks you will get in Sentry "[Can't show request body due to implementation details.]". I found this solution
|
We explicitly ignore all the |
As example custom app error inherited from HTTPException. One of example:
|
I assume that my view can end in error state. But I don't want to show user blank error page. So, I made middleware that do exactly this: async def handle_error(request: Request, code: int = 404) -> Response:
response = aiohttp_jinja2.render_template(f"misc/{code}.html", request, {})
response.set_status(code)
return response
@web.middleware
async def error_middleware(request: Request, handler: Callable) -> Union[FileResponse, Response]:
error_codes = (403, 404, 405, 500)
try:
response = await handler(request)
if response.status not in error_codes:
# return response, everythink is ok
return response
# return error page because handler explicitly set different status code
logging.info("Error: %s", response.status)
return await handle_error(request, response.status)
except web.HTTPException as ex:
# this block for errors generated by aiohttp itself.
# I want to know that something invoke error
# probably I will filter some error codes later
logging.info("Error", exc_info=sys.exc_info())
capture_exception(ex)
if ex.status in errors:
return await handle_error(request, code=ex.status)
# we don't handle this error
raise
except Exception as ex:
# This is place for all other Exceptions, for example ZeroDivisioinError
# This is most important part to capture all traceback
logging.info("Error", exc_info=sys.exc_info())
capture_exception(ex)
return await handle_error(request, code=500) What is preferred way to achieve correct error handling by Sentry? |
Closing this issue is a question. Generally there needs to be a strong case made as to why this behavior should change. Any of the above workarounds is fine as long as it works for you. |
I followed an example https://docs.sentry.io/platforms/python/aiohttp/ and first thing I did
It's work fine and I got this Sentry Issue. But, when you try
you won't get any Sentry Issue.
Is the only way to catch errors to use middleware?
The text was updated successfully, but these errors were encountered: