From be69584637acb68190aed5d03892c710390a8582 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Thu, 22 May 2025 15:18:13 +0400 Subject: [PATCH] Additional error info on the HTTP 500 error page Additional error info (text of the exception thrown by low level C++ code) is shown inside a text box of the same style as used for the advice on the 404 error page (we either need to change the name of the CSS style, or introduce a separate style for this piece of information). --- src/server/response.cpp | 24 +++++++++++++-------- static/templates/sexy500.html | 5 +++++ test/server.cpp | 39 ++++++++++++++++++++++++++--------- 3 files changed, 49 insertions(+), 19 deletions(-) diff --git a/src/server/response.cpp b/src/server/response.cpp index d4caf8f3..900b96c1 100644 --- a/src/server/response.cpp +++ b/src/server/response.cpp @@ -494,21 +494,27 @@ HTTP400Response::HTTP400Response(const RequestContext& request) HTTP500Response::HTTP500Response(const RequestContext& request, const std::string& root, const std::string& urlPath, - const std::string& /*errorText*/) + const std::string& errorText) : ContentResponseBlueprint(&request, MHD_HTTP_INTERNAL_SERVER_ERROR, "text/html; charset=utf-8", RESOURCE::templates::sexy500_html, /*includeKiwixResponseData=*/true) { - *this->m_data = Data(Data::Object{ - {"root", root }, - {"url_path", urlPath}, - {"PAGE_TITLE", Data::fromMsgId("500-page-title")}, - {"PAGE_HEADING", Data::fromMsgId("500-page-heading")}, - {"PAGE_TEXT", Data::fromMsgId("500-page-text")}, - {"500_img_text", Data::fromMsgId("500-img-text")}, - }); + auto pageParams = Data::Object{ + {"root", root }, + {"url_path", urlPath}, + {"PAGE_TITLE", Data::fromMsgId("500-page-title")}, + {"PAGE_HEADING", Data::fromMsgId("500-page-heading")}, + {"PAGE_TEXT", Data::fromMsgId("500-page-text")}, + {"500_img_text", Data::fromMsgId("500-img-text")}, + }; + + if ( !errorText.empty() ) { + pageParams["error"] = errorText; + } + + *this->m_data = Data(pageParams); } std::unique_ptr Response::build_416(size_t resourceLength) diff --git a/static/templates/sexy500.html b/static/templates/sexy500.html index 2ea67c28..1a2d869c 100644 --- a/static/templates/sexy500.html +++ b/static/templates/sexy500.html @@ -22,5 +22,10 @@

{{PAGE_TEXT}}

{{url_path}}

+{{#error}} +
+

{{error}}

+
+{{/error}} diff --git a/test/server.cpp b/test/server.cpp index a7c63ca4..8aa5d0da 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -1210,7 +1210,8 @@ TEST_F(ServerTest, HttpXmlError) } } -std::string expectedSexy500ErrorHtml(const std::string& url) +std::string expectedSexy500ErrorHtml(const std::string& url, + const std::string& error) { const auto urlWithoutQuery = replace(url, "\\?.*$", ""); const auto htmlSafeUrl = htmlEscape(urlWithoutQuery); @@ -1241,8 +1242,12 @@ std::string expectedSexy500ErrorHtml(const std::string& url) )RAWSTRINGLITERAL" + t[0] + R"RAWSTRINGLITERAL(