From a1751967c33b78039fc0b602ea6c408b82346df2 Mon Sep 17 00:00:00 2001 From: Antony Woods Date: Wed, 10 Dec 2014 17:24:13 +0000 Subject: Added a general purpose try-catch around handle() for instances where an exception would cause the server to never return a response. At some later date we would possibly still wish to provide a custom hook for dealing with 500s. Also note, after an exception is caught, the generated 500 response is still sent to middleware handlers --- include/routing.h | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'include/routing.h') diff --git a/include/routing.h b/include/routing.h index 9f607f1..79743cd 100644 --- a/include/routing.h +++ b/include/routing.h @@ -655,7 +655,25 @@ public: CROW_LOG_DEBUG << "Matched rule '" << ((TaggedRule<>*)rules_[rule_index].get())->rule_ << "' " << (uint32_t)req.method << " / " << rules_[rule_index]->methods(); - rules_[rule_index]->handle(req, res, found.second); + // any uncaught exceptions become 500s + try + { + rules_[rule_index]->handle(req, res, found.second); + } + catch(std::exception& e) + { + CROW_LOG_ERROR << "An uncaught exception occurred: " << e.what(); + res = response(500); + res.end(); + return; + } + catch(...) + { + CROW_LOG_ERROR << "An uncaught exception occurred. The type was unknown so no information was available."; + res = response(500); + res.end(); + return; + } } void debug_print() -- cgit v1.2.3-54-g00ecf