Skip to content

Commit e07c5fe

Browse files
ralequiyhirose
authored andcommitted
simplest way to catch handler exceptions
1 parent 6e473a7 commit e07c5fe

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

httplib.h

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3462,14 +3462,23 @@ inline bool Server::routing(Request &req, Response &res, Stream &strm,
34623462

34633463
inline bool Server::dispatch_request(Request &req, Response &res,
34643464
Handlers &handlers) {
3465-
for (const auto &x : handlers) {
3466-
const auto &pattern = x.first;
3467-
const auto &handler = x.second;
34683465

3469-
if (std::regex_match(req.path, req.matches, pattern)) {
3470-
handler(req, res);
3471-
return true;
3466+
try {
3467+
for (const auto &x : handlers) {
3468+
const auto &pattern = x.first;
3469+
const auto &handler = x.second;
3470+
3471+
if (std::regex_match(req.path, req.matches, pattern)) {
3472+
handler(req, res);
3473+
return true;
3474+
}
34723475
}
3476+
} catch (const std::exception &ex) {
3477+
res.status = 500;
3478+
res.set_header("EXCEPTION_WHAT", ex.what());
3479+
} catch (...) {
3480+
res.status = 500;
3481+
res.set_header("EXCEPTION_WHAT", "UNKNOWN");
34733482
}
34743483
return false;
34753484
}

0 commit comments

Comments
 (0)