Skip to content

Commit

Permalink
cathc
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed Oct 12, 2023
1 parent 2c896d1 commit da717ae
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
4 changes: 2 additions & 2 deletions include/cinatra/coro_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions include/cinatra/coro_http_router.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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<void> 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_; }
Expand Down

0 comments on commit da717ae

Please sign in to comment.