def test_errors_raised_in_callback_are_handled(api: API, when): class CustomError(Exception): pass @api.error_handler(CustomError) def handle_error(req, res, exception): res.text = "gotcha!" class MiddlewareWithErrors(Middleware): async def before_dispatch(self, req, res): if when == "before": raise CustomError async def after_dispatch(self, req, res): if when == "after": raise CustomError api.add_middleware(MiddlewareWithErrors) @api.route("/") async def index(req, res): pass client = api.build_client(raise_server_exceptions=False) r = client.get("/") assert r.status_code == 200 assert r.text == "gotcha!"
def test_if_not_debug_then_no_debug_info_returned(api: API): @api.route("/") async def index(req, res): raise ValueError("Oops") client = api.build_client(raise_server_exceptions=False) r = client.get("/") assert r.status_code == 500 assert r.text == "500 Internal Server Error"
def test_debug_response(api: API, accept: str, content_type: str): api.debug = True @api.route("/") async def index(req, res): raise ValueError("Oops") client = api.build_client(raise_server_exceptions=False) r = client.get("/", headers={"accept": accept}) assert r.status_code == 500 assert r.headers["content-type"] == content_type assert 'raise ValueError("Oops")' in r.text