middleware.py 833 B

12345678910111213141516171819202122
  1. import logging
  2. from django.shortcuts import redirect, render
  3. from django.utils.deprecation import MiddlewareMixin
  4. from system.error import ServerException, PermissionDeniedException
  5. class ExceptionMiddleware(MiddlewareMixin):
  6. def process_exception(self, request, exception):
  7. if type(exception) == ServerException:
  8. print("发生异常:{}".format(exception), request)
  9. logging.error("发生异常:{}".format(exception), request)
  10. return render(request, 'error.html', {
  11. 'message': exception.message,
  12. })
  13. elif type(exception) == PermissionDeniedException:
  14. return render(request, 'error.html', {
  15. 'message': exception.message,
  16. })
  17. else:
  18. print("发生异常:{}".format(exception), request)