From 273631e0575d0cee72bab3d2d65398b20868435c Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 10 Feb 2018 15:40:46 +0000 Subject: Default 500 error handler Don't write .what() into the HTTP status header... it might contain things that aren't valid there. Instead, write the class name there and write .what() in the response body. --- icespider/core/core.cpp | 12 +++++++++++- icespider/core/core.h | 3 +++ icespider/unittests/testApp.cpp | 8 +++++--- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/icespider/core/core.cpp b/icespider/core/core.cpp index a822366..d63464e 100644 --- a/icespider/core/core.cpp +++ b/icespider/core/core.cpp @@ -102,7 +102,17 @@ namespace IceSpider { std::cerr << "Error handler failed" << std::endl; } } - request->response(500, exception.what()); + defaultErrorReport(request, exception); + } + + void + Core::defaultErrorReport(IHttpRequest * request, const std::exception & exception) const + { + char * buf = __cxxabiv1::__cxa_demangle(typeid(exception).name(), NULL, NULL, NULL); + request->setHeader("Content-Type", "text/plain"); + request->response(500, buf); + free(buf); + request->getOutputStream() << exception.what(); request->dump(std::cerr); } diff --git a/icespider/core/core.h b/icespider/core/core.h index 4aac640..f358009 100644 --- a/icespider/core/core.h +++ b/icespider/core/core.h @@ -33,6 +33,9 @@ namespace IceSpider { Ice::ObjectAdapterPtr pluginAdapter; static const boost::filesystem::path defaultConfig; + + private: + void defaultErrorReport(IHttpRequest * request, const std::exception & ex) const; }; class DLL_PUBLIC CoreWithDefaultRouter : public Core { diff --git a/icespider/unittests/testApp.cpp b/icespider/unittests/testApp.cpp index 90e7737..5832595 100644 --- a/icespider/unittests/testApp.cpp +++ b/icespider/unittests/testApp.cpp @@ -523,9 +523,11 @@ BOOST_AUTO_TEST_CASE( testErrorHandler_Unhandled ) TestRequest requestDeleteItem(this, HttpMethod::DELETE, "/error"); process(&requestDeleteItem); auto h = requestDeleteItem.getResponseHeaders(); - BOOST_REQUIRE_EQUAL(h["Status"], "500 test error"); - requestDeleteItem.output.get(); - BOOST_REQUIRE(requestDeleteItem.output.eof()); + BOOST_REQUIRE_EQUAL(h["Status"], "500 TestIceSpider::Ex"); + BOOST_REQUIRE_EQUAL(h["Content-Type"], "text/plain"); + auto & o = requestDeleteItem.output; + auto b = o.str().substr(o.tellg()); + BOOST_REQUIRE_EQUAL(b, "test error"); } BOOST_AUTO_TEST_CASE( testErrorHandler_Handled1 ) -- cgit v1.2.3