Skip to content

Commit 6dc285b

Browse files
committed
Merge branch 'kuguma-use_exception_ptr'
2 parents 3e21338 + 07e614e commit 6dc285b

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,15 +212,23 @@ svr.set_error_handler([](const auto& req, auto& res) {
212212
The exception handler gets called if a user routing handler throws an error.
213213

214214
```cpp
215-
svr.set_exception_handler([](const auto& req, auto& res, std::exception &e) {
216-
res.status = 500;
215+
svr.set_exception_handler([](const auto& req, auto& res, std::exception_ptr ep) {
217216
auto fmt = "<h1>Error 500</h1><p>%s</p>";
218217
char buf[BUFSIZ];
219-
snprintf(buf, sizeof(buf), fmt, e.what());
218+
try {
219+
std::rethrow_exception(ep);
220+
} catch (std::exception &e) {
221+
snprintf(buf, sizeof(buf), fmt, e.what());
222+
} catch (...) { // See the following NOTE
223+
snprintf(buf, sizeof(buf), fmt, "Unknown Exception");
224+
}
220225
res.set_content(buf, "text/html");
226+
res.status = 500;
221227
});
222228
```
223229

230+
NOTE: if you don't provide the `catch (...)` block for a rethrown exception pointer, an uncaught exception will end up causing the server crash. Be careful!
231+
224232
### Pre routing handler
225233

226234
```cpp

httplib.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ class Server {
614614
using Handler = std::function<void(const Request &, Response &)>;
615615

616616
using ExceptionHandler =
617-
std::function<void(const Request &, Response &, std::exception &e)>;
617+
std::function<void(const Request &, Response &, std::exception_ptr ep)>;
618618

619619
enum class HandlerResponse {
620620
Handled,
@@ -5721,15 +5721,22 @@ Server::process_request(Stream &strm, bool close_connection,
57215721
routed = routing(req, res, strm);
57225722
} catch (std::exception &e) {
57235723
if (exception_handler_) {
5724-
exception_handler_(req, res, e);
5724+
auto ep = std::current_exception();
5725+
exception_handler_(req, res, ep);
57255726
routed = true;
57265727
} else {
57275728
res.status = 500;
57285729
res.set_header("EXCEPTION_WHAT", e.what());
57295730
}
57305731
} catch (...) {
5731-
res.status = 500;
5732-
res.set_header("EXCEPTION_WHAT", "UNKNOWN");
5732+
if (exception_handler_) {
5733+
auto ep = std::current_exception();
5734+
exception_handler_(req, res, ep);
5735+
routed = true;
5736+
} else {
5737+
res.status = 500;
5738+
res.set_header("EXCEPTION_WHAT", "UNKNOWN");
5739+
}
57335740
}
57345741
#endif
57355742

test/test.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,8 +1249,11 @@ TEST(ExceptionHandlerTest, ContentLength) {
12491249
Server svr;
12501250

12511251
svr.set_exception_handler([](const Request & /*req*/, Response &res,
1252-
std::exception &e) {
1253-
EXPECT_EQ("abc", std::string(e.what()));
1252+
std::exception_ptr ep) {
1253+
EXPECT_FALSE(ep == nullptr);
1254+
try {
1255+
std::rethrow_exception(ep);
1256+
} catch (std::exception &e) { EXPECT_EQ("abc", std::string(e.what())); }
12541257
res.status = 500;
12551258
res.set_content("abcdefghijklmnopqrstuvwxyz",
12561259
"text/html"); // <= Content-Length still 13 at this point

0 commit comments

Comments
 (0)