Skip to content

Instantly share code, notes, and snippets.

@mandarvaze
Created October 15, 2020 16:18
Show Gist options
  • Save mandarvaze/1acf9e133e2ecc94306c7d377623d25f to your computer and use it in GitHub Desktop.
Save mandarvaze/1acf9e133e2ecc94306c7d377623d25f to your computer and use it in GitHub Desktop.

Revisions

  1. mandarvaze created this gist Oct 15, 2020.
    49 changes: 49 additions & 0 deletions hug_honeybadger.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    """Code written to test integration of Honeybadger
    with hug.
    You need to :
    pip install honeybadger
    pip install hug
    Have a trial/free account for Honeybadger
    Set the Honeybadger API key as an environment variable
    HONEYBADGER_API_KEY
    Also set
    export HONEYBADGER_FORCE_REPORT_DATA=True
    else Exception may not be reported to Honeybadger
    Read their documentation to know why
    """

    import hug
    import os

    from honeybadger import Honeybadger

    # Optional Honeybadger integration. It will kick in
    # only if HONEYBADGER_API_KEY environment variable is set
    API_KEY = os.environ.get('HONEYBADGER_API_KEY', None)
    if API_KEY:
    print("Setting up Honeybadger")
    hb = Honeybadger()
    hb.configure(api_key=API_KEY)

    @hug.exception(Exception)
    def handle_exception(exception):
    hb.notify(exception)
    raise exception
    else:
    print("Honeybadger not set, because not API_KEY was set.")


    @hug.get('/happy_birthday')
    def happy_birthday(name, age: hug.types.number = 1):
    """Says happy birthday to a user"""
    return "Happy {age} Birthday {name}!".format(**locals())


    @hug.get('/exception')
    def raise_exception():
    raise Exception("This will be reported")