Add exception handler
This commit is contained in:
		
							parent
							
								
									80650fe6f1
								
							
						
					
					
						commit
						19ab699fa6
					
				
							
								
								
									
										18
									
								
								main.cpp
								
								
								
								
							
							
						
						
									
										18
									
								
								main.cpp
								
								
								
								
							| 
						 | 
					@ -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)) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue