Skip to content

Commit

Permalink
Allow toggling HB reporting for BaseError
Browse files Browse the repository at this point in the history
Set `report = False` in the custom errors (subclass of BaseError) to
skip reporting to Honeybadger.
  • Loading branch information
sayanarijit committed Feb 21, 2023
1 parent fae3648 commit 9b40281
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
4 changes: 4 additions & 0 deletions apphelpers/errors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class BaseError(Exception):

# Whether to report this error to honeybadger
report = True

def __init__(self, **kw):
for k, v in kw.items():
setattr(self, k, v)
Expand Down
51 changes: 32 additions & 19 deletions apphelpers/rest/hug.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from dataclasses import dataclass, asdict
from falcon import HTTPUnauthorized, HTTPForbidden, HTTPNotFound
from dataclasses import asdict, dataclass

import hug
from converge import settings
from falcon import HTTPForbidden, HTTPNotFound, HTTPUnauthorized
from hug.decorators import wraps

from apphelpers.db.peewee import dbtransaction
from apphelpers.errors import InvalidSessionError
from apphelpers.sessions import SessionDBHandler
from converge import settings

from apphelpers.errors import BaseError, InvalidSessionError
from apphelpers.loggers import api_logger
from apphelpers.sessions import SessionDBHandler

if settings.get("HONEYBADGER_API_KEY"):
from honeybadger import Honeybadger
Expand All @@ -33,6 +33,20 @@ def wrapper(*ar, **kw):
return f


def notify_honeybadger(honeybadger, error, func, args, kwargs):
try:
honeybadger.notify(
error,
context={
"func": func.__name__,
"args": args,
"kwargs": filter_dict(kwargs, settings.HB_PARAM_FILTERS),
},
)
finally:
pass


def honeybadger_wrapper(hb):
"""
wrapper that executes the function in a try/except
Expand All @@ -43,20 +57,19 @@ def wrapper(f):
@wraps(f)
def f_wrapped(*args, **kw):
try:
ret = f(*args, **kw)
except Exception as e:
try:
hb.notify(
e,
context={
"func": f.__name__,
"args": args,
"kwargs": filter_dict(kw, settings.HB_PARAM_FILTERS),
},
return f(*args, **kw)
except BaseError as e:
if e.report:
notify_honeybadger(
honeybadger=hb, error=e, func=f, args=args, kwargs=kw
)
finally:
raise e
return ret
raise e

except Exception as e:
notify_honeybadger(
honeybadger=hb, error=e, func=f, args=args, kwargs=kw
)
raise e

return f_wrapped

Expand Down

0 comments on commit 9b40281

Please sign in to comment.