From da717aeb94ac480c73cb47e78d03cc674f1539f6 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Thu, 12 Oct 2023 09:06:38 +0800 Subject: [PATCH] cathc --- include/cinatra/coro_connection.hpp | 4 ++-- include/cinatra/coro_http_router.hpp | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/include/cinatra/coro_connection.hpp b/include/cinatra/coro_connection.hpp index a1a9c84b..e6c61ac6 100644 --- a/include/cinatra/coro_connection.hpp +++ b/include/cinatra/coro_connection.hpp @@ -84,11 +84,11 @@ class coro_http_connection { auto &router = coro_http_router::instance(); if (auto handler = router.get_handler(key); handler) { - (*handler)(request_, response_); + router.route(handler, request_, response_); } else { if (auto coro_handler = router.get_coro_handler(key); coro_handler) { - co_await (*coro_handler)(request_, response_); + co_await router.route_coro(coro_handler, request_, response_); } else { // not found diff --git a/include/cinatra/coro_http_router.hpp b/include/cinatra/coro_http_router.hpp index 2f065551..03dc3464 100644 --- a/include/cinatra/coro_http_router.hpp +++ b/include/cinatra/coro_http_router.hpp @@ -11,6 +11,7 @@ #include "cinatra/cinatra_log_wrapper.hpp" #include "cinatra/coro_http_request.hpp" #include "cinatra/function_traits.hpp" +#include "cinatra/response_cv.hpp" #include "cinatra/utils.hpp" #include "coro_http_response.hpp" @@ -78,6 +79,33 @@ class coro_http_router { return nullptr; } + void route(auto handler, auto& req, auto& resp) { + try { + (*handler)(req, resp); + } catch (const std::exception& e) { + CINATRA_LOG_WARNING << "exception in business function, reason: " + << e.what(); + resp.set_status(status_type::service_unavailable); + } catch (...) { + CINATRA_LOG_WARNING << "unknown exception in business function"; + resp.set_status(status_type::service_unavailable); + } + } + + async_simple::coro::Lazy route_coro(auto handler, auto& req, + auto& resp) { + try { + co_await (*handler)(req, resp); + } catch (const std::exception& e) { + CINATRA_LOG_WARNING << "exception in business function, reason: " + << e.what(); + resp.set_status(status_type::service_unavailable); + } catch (...) { + CINATRA_LOG_WARNING << "unknown exception in business function"; + resp.set_status(status_type::service_unavailable); + } + } + const auto& get_handlers() const { return map_handles_; } const auto& get_coro_handlers() const { return coro_handles_; }