Hide keyboard shortcuts

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 

2 

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 

9 

10# Exports 

11 

12__all__ = ( 

13 "ErrorHandler", 

14) 

15 

16# Classes 

17 

18 

19class ErrorHandler(object): 

20 """Base class for creating an error handler.""" 

21 

22 required_settings = list() 

23 traceback = __import__('traceback') 

24 

25 def __init__(self): 

26 """Initialize the context and check required settings.""" 

27 self.context = dict() 

28 

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))) 

33 

34 def __call__(self, report, context=None): 

35 """Process the exception. 

36 

37 :param report: An exception reporter instance for the current exception(s). 

38 :type report: django.views.debug.ExceptionReporter 

39 

40 :param context: Additional context, especially from any previous error handlers. 

41 :type context: dict 

42 

43 :rtype: dict | HttpResponse | HttpResponseRedirect | HttpResponseServerError 

44 

45 """ 

46 

47 # Add report data to the context. 

48 self.context.update(report.get_traceback_data()) 

49 

50 # Additional context may have been received from previous handlers. 

51 if context is not None: 

52 self.context.update(context) 

53 

54 # Allow the handler to do additional work. 

55 result = self.handle(report) 

56 

57 # Return the result if it is not None. 

58 if result is not None: 

59 return result 

60 

61 # Otherwise return an error response. 

62 template = loader.get_template("500.html") 

63 content = template.render(self.context) 

64 return HttpResponseServerError(content) 

65 

66 # noinspection PyUnusedLocal,PyMethodMayBeStatic 

67 def handle(self, report): 

68 """Provide additional handling of the exception. 

69 

70 :param report: An exception reporter instance for the current exception(s). 

71 :type report: django.views.debug.ExceptionReporter 

72 

73 :rtype: dict | HttpResponse | None 

74 

75 """ 

76 return None 

77 

78 def _parse_template(self, path): 

79 """Parse a given template using the current context. 

80 

81 :param path: The path or name of template. 

82 :type path: str 

83 

84 :rtype: str 

85 

86 """ 

87 return loader.render_to_string(path, self.context) 

88 

89''' 

90class ErrorHandler(object): 

91 """Base class for creating an error handler.""" 

92 

93 required_settings = list() 

94 traceback = __import__('traceback') 

95 

96 def __init__(self): 

97 """Initialize the context and check required settings.""" 

98 self.context = dict() 

99 

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))) 

104 

105 def __call__(self, request, exception, info, context=None): 

106 """Process the exception. 

107 

108 :param report: An exception reporter instance for the current exception(s). 

109 :type report: django.views.debug.ExceptionReporter 

110 

111 :param request: The current request instance. 

112 

113 :param exception: The exception being handled. 

114 

115 :param info: The ``sys.exc_info()`` of the exception. 

116 

117 :param context: Additional context, especially from any previous error handlers. 

118 :type context: dict 

119 

120 :rtype: HttpResponse | HttpResponseRedirect | HttpResponseServerError 

121 

122 """ 

123  

124  

125 # Get the traceback. This produces a list where format_exc() produces a string. 

126 traceback = self.traceback.format_exception(*info) 

127 

128 # Add data to the context. 

129 # self.context.update(report.get_traceback_data()) 

130 self.context.update(self.get_data(request, exception, traceback)) 

131 

132 if context is not None: 

133 self.context.update(context) 

134 

135 # Allow the handler to do additional work. 

136 result = self.handle(request, exception, traceback) 

137 

138 # Return the result if it is itself a response. 

139 if isinstance(result, HttpResponse): 

140 return result 

141 

142 # Otherwise return an error response. 

143 template = loader.get_template("500.html") 

144 content = template.render(self.context) 

145 return HttpResponseServerError(content) 

146 

147 # noinspection PyUnusedLocal,PyMethodMayBeStatic 

148 def get_data(self, request, exception, traceback): 

149 """Get the data to be included in the response context. 

150 

151 :param request: The current request instance. 

152 

153 :param exception: The exception being handled. 

154 

155 :param traceback: The current traceback. 

156 

157 :rtype: dict 

158 

159 """ 

160 if isinstance(exception, Exception): 

161 name = exception.__class__.__name__ 

162 else: 

163 name = exception.__name__ 

164 

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) 

174 

175 return data 

176 

177 # noinspection PyUnusedLocal,PyMethodMayBeStatic 

178 def handle(self, request, exception, traceback): 

179 """Provide additional handling of the exception. 

180 

181 :param request: The current request instance. 

182 

183 :param exception: The exception being handled. 

184 

185 :param traceback: The current traceback (iterable). 

186 

187 :rtype: HttpResponse | None 

188 

189 """ 

190 return None 

191 

192'''