@@ -576,6 +576,7 @@ def __init__(self, host=DEFAULT_LISTEN_HOST, port=DEFAULT_LISTEN_PORT, ssl_conte
576576 self .server = None
577577 self .server_thread = None
578578 self .assertions = []
579+ self .handler_errors = []
579580 self .log = []
580581 self .ordered_handlers = []
581582 self .oneshot_handlers = RequestHandlerList ()
@@ -598,6 +599,7 @@ def clear(self):
598599
599600 """
600601 self .clear_assertions ()
602+ self .clear_handler_errors ()
601603 self .clear_log ()
602604 self .clear_all_handlers ()
603605 self .permanently_failed = False
@@ -610,6 +612,13 @@ def clear_assertions(self):
610612
611613 self .assertions = []
612614
615+ def clear_handler_errors (self ):
616+ """
617+ Clears the list of collected errors from handler invocations
618+ """
619+
620+ self .handler_errors = []
621+
613622 def clear_log (self ):
614623 """
615624 Clears the list of log entries
@@ -893,10 +902,19 @@ def add_assertion(self, obj):
893902 Assertions can be added here, and when :py:meth:`check_assertions` is called,
894903 it will raise AssertionError for pytest with the object specified here.
895904
896- :param obj: An object which will be passed to AssertionError.
905+ :param obj: An AssertionError, or an object which will be passed to an AssertionError.
897906 """
898907 self .assertions .append (obj )
899908
909+ def check (self ):
910+ """
911+ Raises AssertionError or Errors raised in handlers.
912+
913+ Runs both :py:meth:`check_assertions` and :py:meth:`check_handler_errors`
914+ """
915+ self .check_assertions ()
916+ self .check_handler_errors ()
917+
900918 def check_assertions (self ):
901919 """
902920 Raise AssertionError when at least one assertion added
@@ -909,7 +927,21 @@ def check_assertions(self):
909927 """
910928
911929 if self .assertions :
912- raise AssertionError (self .assertions .pop (0 ))
930+ assertion = self .assertions .pop (0 )
931+ if isinstance (assertion , AssertionError ):
932+ raise assertion
933+
934+ raise AssertionError (assertion )
935+
936+ def check_handler_errors (self ):
937+ """
938+ Re-Raises any errors caused in request handlers
939+
940+ The first error raised by a handler will be re-raised here, and then
941+ removed from the list.
942+ """
943+ if self .handler_errors :
944+ raise self .handler_errors .pop (0 )
913945
914946 def format_matchers (self ) -> str :
915947 """
@@ -1007,7 +1039,17 @@ def dispatch(self, request: Request) -> Response:
10071039 if not handler :
10081040 return self .respond_nohandler (request )
10091041
1010- response = handler .respond (request )
1042+ try :
1043+ response = handler .respond (request )
1044+ except Error :
1045+ # don't collect package-internal errors
1046+ raise
1047+ except AssertionError as e :
1048+ self .add_assertion (e )
1049+ raise
1050+ except Exception as e :
1051+ self .handler_errors .append (e )
1052+ raise
10111053
10121054 if response is None :
10131055 response = Response ("" )
0 commit comments