Add exception handler

This commit is contained in:
blankie 2023-04-03 22:43:40 +07:00
parent 80650fe6f1
commit 19ab699fa6
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
1 changed files with 18 additions and 0 deletions

View File

@ -26,10 +26,28 @@ int main(int argc, char** argv) {
}); });
server.Get("/style.css", css_route); server.Get("/style.css", css_route);
#ifndef NDEBUG
server.Get("/debug/exception/known", [](const httplib::Request& req, httplib::Response& res) {
throw std::runtime_error("awoo");
});
server.Get("/debug/exception/unknown", [](const httplib::Request& req, httplib::Response& res) {
throw "cope";
});
#endif
server.Get(".*", [&](const httplib::Request& req, httplib::Response& res) { server.Get(".*", [&](const httplib::Request& req, httplib::Response& res) {
res.status = 404; res.status = 404;
serve_error(req, res, config, "404: Page not found"); serve_error(req, res, config, "404: Page not found");
}); });
server.set_exception_handler([&](const httplib::Request& req, httplib::Response& res, std::exception_ptr ep) {
res.status = 500;
try {
std::rethrow_exception(ep);
} catch (const std::exception& e) {
serve_error(req, res, config, "500: Internal server error", std::nullopt, e.what());
} catch (...) {
serve_error(req, res, config, "500: Internal server error", std::nullopt, "Unknown exception");
}
});
if (config.bind_port != 0) { if (config.bind_port != 0) {
if (!server.bind_to_port(config.bind_host, config.bind_port)) { if (!server.bind_to_port(config.bind_host, config.bind_port)) {