diff --git a/README.md b/README.md
index 48eb423..36acd2a 100644
--- a/README.md
+++ b/README.md
@@ -212,15 +212,23 @@ svr.set_error_handler([](const auto& req, auto& res) {
The exception handler gets called if a user routing handler throws an error.
```cpp
-svr.set_exception_handler([](const auto& req, auto& res, std::exception &e) {
- res.status = 500;
+svr.set_exception_handler([](const auto& req, auto& res, std::exception_ptr ep) {
auto fmt = "
Error 500
%s
";
char buf[BUFSIZ];
- snprintf(buf, sizeof(buf), fmt, e.what());
+ try {
+ std::rethrow_exception(ep);
+ } catch (std::exception &e) {
+ snprintf(buf, sizeof(buf), fmt, e.what());
+ } catch (...) { // See the following NOTE
+ snprintf(buf, sizeof(buf), fmt, "Unknown Exception");
+ }
res.set_content(buf, "text/html");
+ res.status = 500;
});
```
+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!
+
### Pre routing handler
```cpp
diff --git a/httplib.h b/httplib.h
index d02e4c3..2ca8f3b 100644
--- a/httplib.h
+++ b/httplib.h
@@ -614,7 +614,7 @@ public:
using Handler = std::function;
using ExceptionHandler =
- std::function;
+ std::function;
enum class HandlerResponse {
Handled,
@@ -5721,15 +5721,22 @@ Server::process_request(Stream &strm, bool close_connection,
routed = routing(req, res, strm);
} catch (std::exception &e) {
if (exception_handler_) {
- exception_handler_(req, res, e);
+ auto ep = std::current_exception();
+ exception_handler_(req, res, ep);
routed = true;
} else {
res.status = 500;
res.set_header("EXCEPTION_WHAT", e.what());
}
} catch (...) {
- res.status = 500;
- res.set_header("EXCEPTION_WHAT", "UNKNOWN");
+ if (exception_handler_) {
+ auto ep = std::current_exception();
+ exception_handler_(req, res, ep);
+ routed = true;
+ } else {
+ res.status = 500;
+ res.set_header("EXCEPTION_WHAT", "UNKNOWN");
+ }
}
#endif
diff --git a/test/test.cc b/test/test.cc
index aa1d2c8..6ca8497 100644
--- a/test/test.cc
+++ b/test/test.cc
@@ -1249,8 +1249,11 @@ TEST(ExceptionHandlerTest, ContentLength) {
Server svr;
svr.set_exception_handler([](const Request & /*req*/, Response &res,
- std::exception &e) {
- EXPECT_EQ("abc", std::string(e.what()));
+ std::exception_ptr ep) {
+ EXPECT_FALSE(ep == nullptr);
+ try {
+ std::rethrow_exception(ep);
+ } catch (std::exception &e) { EXPECT_EQ("abc", std::string(e.what())); }
res.status = 500;
res.set_content("abcdefghijklmnopqrstuvwxyz",
"text/html"); // <= Content-Length still 13 at this point