mirror of
https://github.com/yhirose/cpp-httplib
synced 2024-11-21 14:29:10 -07:00
simplest way to catch handler exceptions
This commit is contained in:
parent
6e473a7c5c
commit
e07c5fec01
1 changed files with 15 additions and 6 deletions
21
httplib.h
21
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,
|
inline bool Server::dispatch_request(Request &req, Response &res,
|
||||||
Handlers &handlers) {
|
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)) {
|
try {
|
||||||
handler(req, res);
|
for (const auto &x : handlers) {
|
||||||
return true;
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue