Python 3 Django Error or Exception Handling Middleware Using error-tracker Library in Browser article is a very simple article. That means how to learn this article in a simple way, we will see through the article about it.
When you run a public site, you should always turn off the debug setting. This will make your server run faster and prevent malicious users from exposing your application details through error pages.
However, if you run using DEBUG, which is set to error, you’ll never see the errors that are created on your site – instead everyone will see your common error pages. You need to keep track of the errors that occur on the sites used, so you can configure Django to generate reports with details about those errors.
Python 3 Django Error or Exception Handling Middleware Using error-tracker Library in Browser
To install ErrorTracker, simply:
pip install error-tracker
- Sensitive data( like password, secret ) Masking
- Record all the frames ( frame data are stored in JSON format so that it can be analyzed later)
- Unique URL generation
- Number of times the exception occurred and first/last time of exception
- Sending notifications with exception details
- Record different types of exception like 500 or 404 etc
- Raise or update ticket in Jira/Bugzilla etc by ticketing interface.
... APP_ERROR_SEND_EMAIL = True APP_ERROR_RECIPIENT_EMAIL = ('firstname.lastname@example.org',) APP_ERROR_SUBJECT_PREFIX = "Server Error" APP_ERROR_EMAIL_SENDER = 'email@example.com'
from flask import Flask from flask_mail import Mail import settings from error_tracker import AppErrorTracker, NotificationMixin from flask_sqlalchemy import SQLAlchemy ... app = Flask(__name__) app.config.from_object(settings) db = SQLAlchemy(app) class Notifier(Mail, NotificationMixin): def notify(self, request, exception, email_subject=None, email_body=None, from_email=None, recipient_list=None): message = Message(email_subject, recipient_list, email_body, sender=from_email) self.send(message) mailer = Notifier(app=app) error_tracker = AppErrorTracker(app=app, db=db, notifier=mailer) .... .... # Record exception when 404 error code is raised @app.errorhandler(403) def error_403(e): error_tracker.capture_exception() # any custom logic # Record error using decorator @app.errorhandler(500) @error_tracker.track_exception def error_500(e): # some custom logic ....
We need to update settings.py file as
- Add app to installed apps list
- Add Middleware for exception tracking. This should be added at the end so that it can process exception 1st in the middleware call stack.
- Other configs related to notification
... APP_ERROR_RECIPIENT_EMAIL = ('firstname.lastname@example.org',) APP_ERROR_SUBJECT_PREFIX = "Server Error" APP_ERROR_EMAIL_SENDER = 'email@example.com' INSTALLED_APPS = [ ... 'error_tracker.DjangoErrorTracker' ] MIDDLEWARE = [ ... 'error_tracker.django.middleware.ExceptionTrackerMiddleWare' ]
I owe it to you to provide the Python 3 Django Error or Exception Handling Middleware Using error-tracker Library in Browser article in a very simple way. Let us know in the comment box if you need more useful information.