Merge branch 'kuguma-use_exception_ptr'
This commit is contained in:
commit
6dc285b5ca
12
README.md
12
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.
|
The exception handler gets called if a user routing handler throws an error.
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
svr.set_exception_handler([](const auto& req, auto& res, std::exception &e) {
|
svr.set_exception_handler([](const auto& req, auto& res, std::exception_ptr ep) {
|
||||||
res.status = 500;
|
|
||||||
auto fmt = "<h1>Error 500</h1><p>%s</p>";
|
auto fmt = "<h1>Error 500</h1><p>%s</p>";
|
||||||
char buf[BUFSIZ];
|
char buf[BUFSIZ];
|
||||||
|
try {
|
||||||
|
std::rethrow_exception(ep);
|
||||||
|
} catch (std::exception &e) {
|
||||||
snprintf(buf, sizeof(buf), fmt, e.what());
|
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.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
|
### Pre routing handler
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
|
11
httplib.h
11
httplib.h
@ -614,7 +614,7 @@ public:
|
|||||||
using Handler = std::function<void(const Request &, Response &)>;
|
using Handler = std::function<void(const Request &, Response &)>;
|
||||||
|
|
||||||
using ExceptionHandler =
|
using ExceptionHandler =
|
||||||
std::function<void(const Request &, Response &, std::exception &e)>;
|
std::function<void(const Request &, Response &, std::exception_ptr ep)>;
|
||||||
|
|
||||||
enum class HandlerResponse {
|
enum class HandlerResponse {
|
||||||
Handled,
|
Handled,
|
||||||
@ -5721,16 +5721,23 @@ Server::process_request(Stream &strm, bool close_connection,
|
|||||||
routed = routing(req, res, strm);
|
routed = routing(req, res, strm);
|
||||||
} catch (std::exception &e) {
|
} catch (std::exception &e) {
|
||||||
if (exception_handler_) {
|
if (exception_handler_) {
|
||||||
exception_handler_(req, res, e);
|
auto ep = std::current_exception();
|
||||||
|
exception_handler_(req, res, ep);
|
||||||
routed = true;
|
routed = true;
|
||||||
} else {
|
} else {
|
||||||
res.status = 500;
|
res.status = 500;
|
||||||
res.set_header("EXCEPTION_WHAT", e.what());
|
res.set_header("EXCEPTION_WHAT", e.what());
|
||||||
}
|
}
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
|
if (exception_handler_) {
|
||||||
|
auto ep = std::current_exception();
|
||||||
|
exception_handler_(req, res, ep);
|
||||||
|
routed = true;
|
||||||
|
} else {
|
||||||
res.status = 500;
|
res.status = 500;
|
||||||
res.set_header("EXCEPTION_WHAT", "UNKNOWN");
|
res.set_header("EXCEPTION_WHAT", "UNKNOWN");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (routed) {
|
if (routed) {
|
||||||
|
@ -1249,8 +1249,11 @@ TEST(ExceptionHandlerTest, ContentLength) {
|
|||||||
Server svr;
|
Server svr;
|
||||||
|
|
||||||
svr.set_exception_handler([](const Request & /*req*/, Response &res,
|
svr.set_exception_handler([](const Request & /*req*/, Response &res,
|
||||||
std::exception &e) {
|
std::exception_ptr ep) {
|
||||||
EXPECT_EQ("abc", std::string(e.what()));
|
EXPECT_FALSE(ep == nullptr);
|
||||||
|
try {
|
||||||
|
std::rethrow_exception(ep);
|
||||||
|
} catch (std::exception &e) { EXPECT_EQ("abc", std::string(e.what())); }
|
||||||
res.status = 500;
|
res.status = 500;
|
||||||
res.set_content("abcdefghijklmnopqrstuvwxyz",
|
res.set_content("abcdefghijklmnopqrstuvwxyz",
|
||||||
"text/html"); // <= Content-Length still 13 at this point
|
"text/html"); // <= Content-Length still 13 at this point
|
||||||
|
Loading…
x
Reference in New Issue
Block a user