From e07c5fec015c19ea96305bb8edc3859f8eabff0e Mon Sep 17 00:00:00 2001 From: Rafael Leira Date: Tue, 10 Mar 2020 13:20:26 +0100 Subject: [PATCH] simplest way to catch handler exceptions --- httplib.h | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/httplib.h b/httplib.h index f3a2447..b5fdbd4 100644 --- a/httplib.h +++ b/httplib.h @@ -3462,14 +3462,23 @@ inline bool Server::routing(Request &req, Response &res, Stream &strm, inline bool Server::dispatch_request(Request &req, Response &res, Handlers &handlers) { - for (const auto &x : handlers) { - const auto &pattern = x.first; - const auto &handler = x.second; - if (std::regex_match(req.path, req.matches, pattern)) { - handler(req, res); - return true; + try { + for (const auto &x : handlers) { + const auto &pattern = x.first; + const auto &handler = x.second; + + if (std::regex_match(req.path, req.matches, pattern)) { + handler(req, res); + return true; + } } + } catch (const std::exception &ex) { + res.status = 500; + res.set_header("EXCEPTION_WHAT", ex.what()); + } catch (...) { + res.status = 500; + res.set_header("EXCEPTION_WHAT", "UNKNOWN"); } return false; }