Edgewall Software

Ticket #3328: trap_early_errors-r3549.diff

File trap_early_errors-r3549.diff, 3.5 KB (added by cboos, 2 years ago)

Second attempt, a bit more robust.

  • trac/web/main.py

     
    161161 
    162162        # Select the component that should handle the request 
    163163        chosen_handler = None 
    164         if not req.path_info or req.path_info == '/': 
    165             chosen_handler = self.default_handler 
    166         else: 
    167             for handler in self.handlers: 
    168                 if handler.match_request(req): 
    169                     chosen_handler = handler 
    170                     break 
     164        early_error = None 
     165        try: 
     166            if not req.path_info or req.path_info == '/': 
     167                chosen_handler = self.default_handler 
     168            else: 
     169                for handler in self.handlers: 
     170                    if handler.match_request(req): 
     171                        chosen_handler = handler 
     172                        break 
    171173 
    172         for filter_ in self.filters: 
    173             chosen_handler = filter_.pre_process_request(req, chosen_handler) 
     174            for f in self.filters: 
     175                chosen_handler = f.pre_process_request(req, chosen_handler) 
     176                 
     177        except Exception, e: 
     178            early_error = (e, sys.exc_traceback) 
     179             
     180        if not chosen_handler and not early_error: 
     181            early_error = (HTTPNotFound('No handler matched request to %s', 
     182                                        req.path_info), None) 
    174183 
    175         if not chosen_handler: 
    176             raise HTTPNotFound('No handler matched request to %s', 
    177                                req.path_info) 
    178  
    179184        # Attach user information to the request 
    180         anonymous_request = getattr(chosen_handler, 'anonymous_request', False) 
     185        anonymous_request = getattr(chosen_handler, 'anonymous_request', 
     186                                    False) 
     187        if not anonymous_request: 
     188            try: 
     189                req.authname = self.authenticate(req) 
     190                req.perm = PermissionCache(self.env, req.authname) 
     191                req.session = Session(self.env, req) 
     192            except Exception, e: 
     193                anonymous_request = True 
     194                early_error = (e, sys.exc_traceback) 
    181195        if anonymous_request: 
    182196            req.authname = 'anonymous' 
    183197            req.perm = NoPermissionCache() 
    184         else: 
    185             req.authname = self.authenticate(req) 
    186             req.perm = PermissionCache(self.env, req.authname) 
    187             req.session = Session(self.env, req) 
    188198 
    189199        # Prepare HDF for the clearsilver template 
    190         use_template = getattr(chosen_handler, 'use_template', True) 
    191         if use_template: 
    192             chrome = Chrome(self.env) 
    193             req.hdf = HDFWrapper(loadpaths=chrome.get_all_templates_dirs()) 
    194             populate_hdf(req.hdf, self.env, req) 
    195             chrome.populate_hdf(req, chosen_handler) 
     200        try: 
     201            use_template = getattr(chosen_handler, 'use_template', True) 
     202            if use_template: 
     203                chrome = Chrome(self.env) 
     204                req.hdf = HDFWrapper(loadpaths=chrome.get_all_templates_dirs()) 
     205                populate_hdf(req.hdf, self.env, req) 
     206                chrome.populate_hdf(req, chosen_handler) 
     207        except Exception, e: 
     208            req.hdf = None # revert to sending plaintext error 
     209            if not early_error: 
     210                raise e, None, sys.exc_traceback 
    196211 
     212        if early_error: 
     213            raise early_error[0], None, early_error[1] 
     214 
    197215        # Process the request and render the template 
    198216        try: 
    199217            try: