Edgewall Software

Ticket #3655: irequestfilter_patch_3689.3.diff

File irequestfilter_patch_3689.3.diff, 2.9 KB (added by cboos, 6 years ago)

Also post-process a request in case of early errors or errors while dispatching the request

  • trac/web/api.py

     
    467467    requests, before and/or after they are processed by the main handler.""" 
    468468 
    469469    def pre_process_request(req, handler): 
    470         """Do any pre-processing the request might need; typically adding 
    471         values to req.hdf, or redirecting. 
     470        """Called after initial handler selection, and can be used to change 
     471        the selected handler or redirect request. 
    472472         
    473473        Always returns the request handler, even if unchanged. 
    474474        """ 
  • trac/web/main.py

     
    201201        # Prepare HDF for the clearsilver template 
    202202        try: 
    203203            use_template = getattr(chosen_handler, 'use_template', True) 
     204            req.hdf = None 
    204205            if use_template: 
    205206                chrome = Chrome(self.env) 
    206207                req.hdf = HDFWrapper(loadpaths=chrome.get_all_templates_dirs()) 
     
    211212            if not early_error: 
    212213                raise 
    213214 
     215        def post_process_request(resp=(None, None)): 
     216            for filter_ in reversed(self.filters): 
     217                resp = filter_.post_process_request(req, *resp) 
     218            return resp 
     219 
     220        def post_process_after_error(): 
     221            try: 
     222                post_process_request() 
     223            except Exception, e: 
     224                self.log.exception(e) 
     225 
    214226        if early_error: 
     227            post_process_after_error() 
    215228            raise early_error[0], early_error[1], early_error[2] 
    216229 
    217230        # Process the request and render the template 
    218231        try: 
    219232            try: 
    220                 resp = chosen_handler.process_request(req) 
    221                 if resp: 
    222                     for filter_ in reversed(self.filters): 
    223                         resp = filter_.post_process_request(req, *resp) 
    224                     template, content_type = resp 
    225                     req.display(template, content_type or 'text/html') 
    226                 else: 
    227                     for filter_ in reversed(self.filters): 
    228                         filter_.post_process_request(req, None, None) 
     233                try: 
     234                    resp = chosen_handler.process_request(req) 
     235                    if resp: 
     236                        template, content_type = post_process_request(resp) 
     237                        req.display(template, content_type or 'text/html') 
     238                    else: 
     239                        post_process_request() 
     240                except: 
     241                    post_process_after_error() 
     242                    err = sys.exc_info() 
     243                    raise err[0], err[1], err[2] 
    229244            except PermissionError, e: 
    230245                raise HTTPForbidden(to_unicode(e)) 
    231246            except TracError, e: