class ExceptionHandlingMiddleware: """ 해당 예외에 대해서 등록한 뷰함수를 그 예외가 발생하면 실행하도록. 1. settings.MIDDLEWARES에 등록하기 2. ExceptionHandlingMiddleware.append(...)으로 예외클래스에 대해서 뷰함수 등록하기 """ # __exception_handlers__ = [ # (exception_type, handling_view_function, args, kwargs) ] @classmethod def append(klass, exc_type, handling_viewfunc, args=[], kwargs={}): """ 주어진 exc_type의 예외에 대해서 handling_viewfunc로 응답을 되돌리도록 등록하기 """ # if exc_type in [ t[0] for t in klass.__exception_handlers__ ]: raise ValueError("exception '%s' is already had handler(%s)" % \ (str(exc_type), str(handling_viewfunc))) # t = (exc_type, handling_viewfunc, args, kwargs) return klass.__exception_handlers__.append(t) def process_exception(self, request, exception): for t in ExceptionHandlingMiddleware.__exception_handlers__: exc_type, handler, args, kwargs = t if isinstance(exception, exc_type): kwargs2 = kwargs.copy() kwargs2['exception_type'] = exc_type kwargs2['exception'] = exception return handler(request, *args, **kwargs2) return None