Commits

Alexander Shorin committed 4fbe304

Try to probe all rules before respond with an error.

  • Participants
  • Parent commits 42de604

Comments (0)

Files changed (1)

         self.rules.append(rule)
 
     def dispatch(self, req):
+        errors = []
         for rule in self.rules:
             try:
                 m = rule.match(req)
-            except MethodNotAllowed as err:
-                return self.make_error(405, 'method_not_allowed', str(err), {})
-            except PathNotFound as err:
-                return self.make_error(404, 'not_found', str(err), {})
-            except NotAcceptable as err:
-                return self.make_error(400, 'not_found', str(err), {})
+            except RuleMatchException as err:
+                if isinstance(err, MethodNotAllowed):
+                    code, name = 405, 'method_not_allowed'
+                elif isinstance(err, PathNotFound):
+                    code, name = 404, 'not_found'
+                elif isinstance(err, NotAcceptable):
+                    code, name = 406, 'not_acceptable'
+                errors.append(self.make_error(code, name, str(err), {}))
+                continue
             if m is not None:
                 func, kwargs = m
                 accepted_kwargs = dict(
                     if key in func.func_code.co_varnames
                 )
                 return func(**accepted_kwargs)
-        return self.make_error(404, 'not_found', 'missed', {})
+        if errors:
+            errors = sorted(errors, key=lambda e: e['code'])
+            return errors[-1]
+        else:
+            return self.make_error(404, 'not_found', 'missed', {})
 
     def make_response(self, code, data, headers=None):
         headers = headers or {}