Skip to content

Commit 4247f3d

Browse files
committed
server : add try..catch to places not covered by set_exception_handler
1 parent 8ec0583 commit 4247f3d

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

examples/server/server.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3353,10 +3353,14 @@ static void log_server_request(const httplib::Request & req, const httplib::Resp
33533353
return;
33543354
}
33553355

3356-
LOG_INF("request: %s %s %s %d\n", req.method.c_str(), req.path.c_str(), req.remote_addr.c_str(), res.status);
3356+
try {
3357+
LOG_INF("request: %s %s %s %d\n", req.method.c_str(), req.path.c_str(), req.remote_addr.c_str(), res.status);
33573358

3358-
LOG_DBG("request: %s\n", req.body.c_str());
3359-
LOG_DBG("response: %s\n", res.body.c_str());
3359+
LOG_DBG("request: %s\n", req.body.c_str());
3360+
LOG_DBG("response: %s\n", res.body.c_str());
3361+
} catch (const std::exception & e) {
3362+
LOG_ERR("failed to log request/response: %s\n", e.what());
3363+
}
33603364
}
33613365

33623366
std::function<void(int)> shutdown_handler;
@@ -3439,9 +3443,13 @@ int main(int argc, char ** argv) {
34393443
message = "Unknown Exception";
34403444
}
34413445

3442-
json formatted_error = format_error_response(message, ERROR_TYPE_SERVER);
3443-
LOG_WRN("got exception: %s\n", formatted_error.dump().c_str());
3444-
res_error(res, formatted_error);
3446+
try {
3447+
json formatted_error = format_error_response(message, ERROR_TYPE_SERVER);
3448+
LOG_WRN("got exception: %s\n", formatted_error.dump().c_str());
3449+
res_error(res, formatted_error);
3450+
} catch (const std::exception & e) {
3451+
LOG_ERR("got another exception: %s | while hanlding exception: %s\n", e.what(), message.c_str());
3452+
}
34453453
});
34463454

34473455
svr->set_error_handler([&res_error](const httplib::Request &, httplib::Response & res) {

0 commit comments

Comments
 (0)