1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Closes #1683: Replaced default 500 handler with custom middleware to provide preliminary troubleshooting assistance

This commit is contained in:
Jeremy Stretch
2017-11-03 13:24:31 -04:00
parent f2fbd92f78
commit f77bf72de8
8 changed files with 101 additions and 32 deletions

View File

@ -1,7 +1,10 @@
from __future__ import unicode_literals
import sys
from django.http import HttpResponseRedirect
from django.conf import settings
from django.db import ProgrammingError
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
@ -39,3 +42,38 @@ class APIVersionMiddleware(object):
if request.path_info.startswith(api_path):
response['API-Version'] = settings.REST_FRAMEWORK_VERSION
return response
class ExceptionHandlingMiddleware(object):
"""
Intercept certain exceptions which are likely indicative of installation issues and provide helpful instructions
to the user.
"""
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
return self.get_response(request)
def process_exception(self, request, exception):
# Raise exceptions if in debug mode
if settings.DEBUG:
raise exception
# Determine the type of exception
if isinstance(exception, ProgrammingError):
template_name = 'exceptions/programming_error.html'
elif isinstance(exception, ImportError):
template_name = 'exceptions/import_error.html'
elif isinstance(exception, PermissionError):
template_name = 'exceptions/permission_error.html'
else:
template_name = '500.html'
# Return an error message
type_, error, traceback = sys.exc_info()
return render(request, template_name, {
'exception': str(type_),
'error': error,
}, status=500)