Edgewall Software

Ticket #3328: trap_early_errors-r3491.diff

File trap_early_errors-r3491.diff, 3.0 KB (added by cboos, 2 years ago)

Catch early errors in request dispatching, and re-raise them once the HDFWrapper is created.

  • trac/web/main.py

     
    160160 
    161161        # Select the component that should handle the request 
    162162        chosen_handler = None 
    163         if not req.path_info or req.path_info == '/': 
    164             chosen_handler = self.default_handler 
    165         else: 
    166             for handler in self.handlers: 
    167                 if handler.match_request(req): 
    168                     chosen_handler = handler 
    169                     break 
     163        early_error = None 
     164        try: 
     165            if not req.path_info or req.path_info == '/': 
     166                chosen_handler = self.default_handler 
     167            else: 
     168                for handler in self.handlers: 
     169                    if handler.match_request(req): 
     170                        chosen_handler = handler 
     171                        break 
    170172 
    171         for filter_ in self.filters: 
    172             chosen_handler = filter_.pre_process_request(req, chosen_handler) 
     173            for filter_ in self.filters: 
     174                chosen_handler = filter_.pre_process_request(req, 
     175                                                             chosen_handler) 
     176        except Exception, e: 
     177            early_error = (e, sys.exc_traceback) 
     178             
     179        if not chosen_handler and not early_error: 
     180            early_error = (HTTPNotFound('No handler matched request to %s', 
     181                                        req.path_info), None) 
    173182 
    174         if not chosen_handler: 
    175             raise HTTPNotFound('No handler matched request to %s', 
    176                                req.path_info) 
    177  
    178183        # Attach user information to the request 
    179         anonymous_request = getattr(chosen_handler, 'anonymous_request', False) 
    180         if anonymous_request: 
    181             req.authname = 'anonymous' 
    182             req.perm = NoPermissionCache() 
    183         else: 
    184             req.authname = self.authenticate(req) 
    185             req.perm = PermissionCache(self.env, req.authname) 
    186             req.session = Session(self.env, req) 
     184        try: 
     185            anonymous_request = getattr(chosen_handler, 'anonymous_request', 
     186                                        False) 
     187            if anonymous_request: 
     188                req.authname = 'anonymous' 
     189                req.perm = NoPermissionCache() 
     190            else: 
     191                req.authname = self.authenticate(req) 
     192                req.perm = PermissionCache(self.env, req.authname) 
     193                req.session = Session(self.env, req) 
     194        except Exception, e: 
     195            early_error = (e, sys.exc_traceback) 
    187196 
    188197        # Prepare HDF for the clearsilver template 
    189198        use_template = getattr(chosen_handler, 'use_template', True) 
     
    193202            populate_hdf(req.hdf, self.env, req) 
    194203            chrome.populate_hdf(req, chosen_handler) 
    195204 
     205        if early_error: 
     206            raise early_error[0], None, early_error[1] 
     207 
    196208        # Process the request and render the template 
    197209        try: 
    198210            try: