devilry / devilry-django

Devilry project main repository
http://devilry.org
BSD 3-Clause "New" or "Revised" License
51 stars 24 forks source link

Graceful handling of preview input errors #1271

Closed torgeirl closed 1 year ago

torgeirl commented 1 year ago

Similar to #1228, ValueError from /devilry_comment/_api/preview-markdown (comment message previews failing due to syntax errors in code/markdown input) doesn't provide any actionable data.

From the user's perspective it is handled well, but from the system side this error is unnecessary and shouldn't be generated.

torgeirl commented 1 year ago

(...) from the system side this error is unnecessary and shouldn't be generated.

Fixed by preventing the exception from being forwarded to Sentry with before_send filtering:

from urllib.parse import urlparse
from sentry_sdk import init as sentry_init

(...)

def before_send(event, hint):
    '''Ignore errors caused by faulty preview input.'''
    url_string = event['request']['url']
    parsed_url = urlparse(url_string)

    if parsed_url.path == '/devilry_comment/_api/preview-markdown':
        if 'exc_info' in hint:
            exc_type, exc_value, tb = hint['exc_info']
            if isinstance(exc_value, ValueError):
                return None
    return event

(...)

sentry_init(
    (...)
    before_send=before_send,
    (...)