Skip to content

Commit

Permalink
Merge PR #2688 into 16.0
Browse files Browse the repository at this point in the history
Signed-off-by moylop260
  • Loading branch information
OCA-git-bot committed Jul 31, 2023
2 parents 2f8f08f + f86bbf4 commit ab7a2ee
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
20 changes: 19 additions & 1 deletion sentry/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,26 @@


def before_send(event, hint):
"""Add context to event if include_context is True
"""Prevent the capture of any exceptions in
the DEFAULT_IGNORED_EXCEPTIONS list
-- or --
Add context to event if include_context is True
and sanitize sensitive data"""

exc_info = hint.get("exc_info")
if exc_info is None and "log_record" in hint:
# Odoo handles UserErrors by logging the raw exception rather
# than a message string in odoo/http.py
try:
module_name = hint["log_record"].msg.__module__
class_name = hint["log_record"].msg.__class__.__name__
qualified_name = module_name + "." + class_name
except AttributeError:
qualified_name = "not found"

if qualified_name in const.DEFAULT_IGNORED_EXCEPTIONS:
return None

if event.setdefault("tags", {})["include_context"]:
cxtest = get_extra_context(odoo.http.request)
info_request = ["tags", "user", "extra", "request"]
Expand Down
2 changes: 2 additions & 0 deletions sentry/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
* Naglis Jonaitis <naglis@versada.eu>
* Atte Isopuro <atte.isopuro@avoin.systems>
* Florian Mounier <florian.mounier@akretion.com>
* Jon Ashton <jon@monkeyinferno.com>
* Mark Schuit <mark@gig.solutions>
30 changes: 30 additions & 0 deletions sentry/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
GIT_SHA = "d670460b4b4aece5915caf5c68d12f560a9fe3e4"
RELEASE = "test@1.2.3"

_logger = logging.getLogger(__name__)


def remove_handler_ignore(handler_name):
"""Removes handlers of handlers ignored list."""
Expand Down Expand Up @@ -118,6 +120,33 @@ def test_ignore_exceptions(self):
level = "warning"
self.assertEventNotCaptured(client, level, msg)

def test_capture_exceptions_with_no_exc_info(self):
"""A UserError that isn't in the DEFAULT_IGNORED_EXCEPTIONS list is captured
(there is no exc_info in the ValidationError exception)."""
client = initialize_sentry(config)._client
client.transport = InMemoryTransport({"dsn": self.dsn})
level, msg = logging.WARNING, "Test exception"

# Odoo handles UserErrors by logging the exception
with patch("odoo.addons.sentry.const.DEFAULT_IGNORED_EXCEPTIONS", new=[]):
_logger.warning(exceptions.ValidationError(msg))

level = "warning"
self.assertEventCaptured(client, level, msg)

def test_ignore_exceptions_with_no_exc_info(self):
"""A UserError that is in the DEFAULT_IGNORED_EXCEPTIONS is not captured
(there is no exc_info in the ValidationError exception)."""
client = initialize_sentry(config)._client
client.transport = InMemoryTransport({"dsn": self.dsn})
level, msg = logging.WARNING, "Test exception"

# Odoo handles UserErrors by logging the exception
_logger.warning(exceptions.ValidationError(msg))

level = "warning"
self.assertEventNotCaptured(client, level, msg)

def test_exclude_logger(self):
config.options["sentry_enabled"] = True
config.options["sentry_exclude_loggers"] = __name__
Expand All @@ -128,6 +157,7 @@ def test_exclude_logger(self):
level = "warning"
# Revert ignored logger so it doesn't affect other tests
remove_handler_ignore(__name__)
del config.options["sentry_exclude_loggers"]
self.assertEventNotCaptured(client, level, msg)

@patch("odoo.addons.sentry.hooks.get_odoo_commit", return_value=GIT_SHA)
Expand Down

0 comments on commit ab7a2ee

Please sign in to comment.