Last active
March 9, 2020 19:46
-
-
Save alertedsnake/638ebc5158c1b43401e7e5223b131d8e to your computer and use it in GitHub Desktop.
sentry exception tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sentry_sdk | |
import MySQLdb | |
import logging | |
import os | |
from sentry_sdk.integrations.logging import LoggingIntegration | |
DB_HOST = 'bogus.example.com' | |
DB_USER = 'monkey' | |
DB_PASS = 'you know what this is' | |
DB_DB = 'database' | |
logging.basicConfig(level=logging.INFO) | |
log = logging.getLogger(__name__) | |
def init(): | |
sentry_sdk.init( | |
os.getenv('SENTRY_DSN'), | |
integrations = [ | |
LoggingIntegration(level=logging.INFO, event_level=logging.ERROR), | |
]) | |
def log_error_test(): | |
""" | |
This test logs an error. This is the safe case. | |
""" | |
init() | |
try: | |
MySQLdb.connect(DB_HOST, DB_USER, DB_PASS, DB_DB, 3306, connect_timeout=300) | |
except MySQLdb.OperationalError as e: | |
log.error("Database connection error: {}".format(e)) | |
def log_exception_test(): | |
""" | |
This test logs as an exception, which causes Sentry to expose the database | |
credentials. | |
""" | |
init() | |
try: | |
MySQLdb.connect(DB_HOST, DB_USER, DB_PASS, DB_DB, 3306, connect_timeout=300) | |
except MySQLdb.OperationalError: | |
log.exception("Database connection error") | |
def abort_test(): | |
""" | |
This case just raises the raw exception, letting the Sentry library handle it. | |
This will expose the database credentials. | |
""" | |
init() | |
MySQLdb.connect(DB_HOST, DB_USER, DB_PASS, DB_DB, 3306, connect_timeout=300) | |
if __name__ == '__main__': | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument('mode', choices=('log-error', 'log-exception', 'abort')) | |
args = parser.parse_args() | |
if args.mode == 'log-error': | |
log_error_test() | |
elif args.mode == 'log-exception': | |
log_error_test() | |
else: | |
abort_test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment