Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Imports
3from django.conf import settings
4from django.core.exceptions import ImproperlyConfigured
5from django.http import HttpResponse, HttpResponseServerError
6from django.template import loader
7from django.views.debug import ExceptionReporter
8import socket
10# Exports
12__all__ = (
13 "ErrorHandler",
14)
16# Classes
19class ErrorHandler(object):
20 """Base class for creating an error handler."""
22 required_settings = list()
23 traceback = __import__('traceback')
25 def __init__(self):
26 """Initialize the context and check required settings."""
27 self.context = dict()
29 message = "The following settings are required for the %s backend: %s"
30 for required_setting in self.required_settings:
31 if not hasattr(settings, required_setting):
32 raise ImproperlyConfigured(message % (self.__class__.__name__, ", ".join(self.required_settings)))
34 def __call__(self, report, context=None):
35 """Process the exception.
37 :param report: An exception reporter instance for the current exception(s).
38 :type report: django.views.debug.ExceptionReporter
40 :param context: Additional context, especially from any previous error handlers.
41 :type context: dict
43 :rtype: dict | HttpResponse | HttpResponseRedirect | HttpResponseServerError
45 """
47 # Add report data to the context.
48 self.context.update(report.get_traceback_data())
50 # Additional context may have been received from previous handlers.
51 if context is not None:
52 self.context.update(context)
54 # Allow the handler to do additional work.
55 result = self.handle(report)
57 # Return the result if it is not None.
58 if result is not None:
59 return result
61 # Otherwise return an error response.
62 template = loader.get_template("500.html")
63 content = template.render(self.context)
64 return HttpResponseServerError(content)
66 # noinspection PyUnusedLocal,PyMethodMayBeStatic
67 def handle(self, report):
68 """Provide additional handling of the exception.
70 :param report: An exception reporter instance for the current exception(s).
71 :type report: django.views.debug.ExceptionReporter
73 :rtype: dict | HttpResponse | None
75 """
76 return None
78 def _parse_template(self, path):
79 """Parse a given template using the current context.
81 :param path: The path or name of template.
82 :type path: str
84 :rtype: str
86 """
87 return loader.render_to_string(path, self.context)
89'''
90class ErrorHandler(object):
91 """Base class for creating an error handler."""
93 required_settings = list()
94 traceback = __import__('traceback')
96 def __init__(self):
97 """Initialize the context and check required settings."""
98 self.context = dict()
100 message = "The following settings are required for the %s backend: %s"
101 for required_setting in self.required_settings:
102 if not hasattr(settings, required_setting):
103 raise ImproperlyConfigured(message % (self.__class__.__name__, ", ".join(self.required_settings)))
105 def __call__(self, request, exception, info, context=None):
106 """Process the exception.
108 :param report: An exception reporter instance for the current exception(s).
109 :type report: django.views.debug.ExceptionReporter
111 :param request: The current request instance.
113 :param exception: The exception being handled.
115 :param info: The ``sys.exc_info()`` of the exception.
117 :param context: Additional context, especially from any previous error handlers.
118 :type context: dict
120 :rtype: HttpResponse | HttpResponseRedirect | HttpResponseServerError
122 """
125 # Get the traceback. This produces a list where format_exc() produces a string.
126 traceback = self.traceback.format_exception(*info)
128 # Add data to the context.
129 # self.context.update(report.get_traceback_data())
130 self.context.update(self.get_data(request, exception, traceback))
132 if context is not None:
133 self.context.update(context)
135 # Allow the handler to do additional work.
136 result = self.handle(request, exception, traceback)
138 # Return the result if it is itself a response.
139 if isinstance(result, HttpResponse):
140 return result
142 # Otherwise return an error response.
143 template = loader.get_template("500.html")
144 content = template.render(self.context)
145 return HttpResponseServerError(content)
147 # noinspection PyUnusedLocal,PyMethodMayBeStatic
148 def get_data(self, request, exception, traceback):
149 """Get the data to be included in the response context.
151 :param request: The current request instance.
153 :param exception: The exception being handled.
155 :param traceback: The current traceback.
157 :rtype: dict
159 """
160 if isinstance(exception, Exception):
161 name = exception.__class__.__name__
162 else:
163 name = exception.__name__
165 data = {
166 'GET': request.GET,
167 'POST': request.POST,
168 'SERVER_HOSTNAME': socket.gethostname(),
169 'error_name': name,
170 'request_path': request.get_full_path(),
171 'traceback': traceback,
172 }
173 data.update(request.META)
175 return data
177 # noinspection PyUnusedLocal,PyMethodMayBeStatic
178 def handle(self, request, exception, traceback):
179 """Provide additional handling of the exception.
181 :param request: The current request instance.
183 :param exception: The exception being handled.
185 :param traceback: The current traceback (iterable).
187 :rtype: HttpResponse | None
189 """
190 return None
192'''