mirror of
https://github.com/kiwix/libkiwix.git
synced 2025-08-03 10:16:03 -04:00
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).
This commit is contained in:
parent
2ba29f76e1
commit
be69584637
@ -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> Response::build_416(size_t resourceLength)
|
||||
|
@ -22,5 +22,10 @@
|
||||
<p>{{PAGE_TEXT}}</p>
|
||||
<p><code>{{url_path}}</code></p>
|
||||
</section>
|
||||
{{#error}}
|
||||
<section class="advice">
|
||||
<p>{{error}}</p>
|
||||
</section>
|
||||
{{/error}}
|
||||
</body>
|
||||
</html>
|
||||
|
@ -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)
|
||||
<title>)RAWSTRINGLITERAL" + t[0] + R"RAWSTRINGLITERAL(</title>
|
||||
<link type="text/css" href="/ROOT%23%3F/skin/error.css?cacheid=b3fa90cf" rel="Stylesheet" />
|
||||
<script>
|
||||
window.KIWIX_RESPONSE_TEMPLATE = "<!DOCTYPE html>\n<html>\n <head>\n <meta charset="utf-8">\n <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />\n <title>{{PAGE_TITLE}}</title>\n <link type="text/css" href="{{root}}/skin/error.css?cacheid=b3fa90cf" rel="Stylesheet" />\n <script>\n window.KIWIX_RESPONSE_TEMPLATE = "{{KIWIX_RESPONSE_TEMPLATE}}";\n window.KIWIX_RESPONSE_DATA = {{{KIWIX_RESPONSE_DATA}}};\n </script>\n </head>\n <body>\n <header>\n <img src="{{root}}/skin/500.svg?cacheid=32eb0f20"\n alt="{{500_img_text}}"\n aria-label="{{500_img_text}}"\n title="{{500_img_text}}">\n </header>\n <section class="intro">\n <h1>{{PAGE_HEADING}}</h1>\n <p>{{PAGE_TEXT}}</p>\n <p><code>{{url_path}}</code></p>\n </section>\n </body>\n</html>\n";
|
||||
window.KIWIX_RESPONSE_DATA = { "500_img_text" : { "msgid" : "500-img-text", "params" : { } }, "PAGE_HEADING" : { "msgid" : "500-page-heading", "params" : { } }, "PAGE_TEXT" : { "msgid" : "500-page-text", "params" : { } }, "PAGE_TITLE" : { "msgid" : "500-page-title", "params" : { } }, "root" : "/ROOT%23%3F", "url_path" : ")RAWSTRINGLITERAL"
|
||||
window.KIWIX_RESPONSE_TEMPLATE = "<!DOCTYPE html>\n<html>\n <head>\n <meta charset="utf-8">\n <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />\n <title>{{PAGE_TITLE}}</title>\n <link type="text/css" href="{{root}}/skin/error.css?cacheid=b3fa90cf" rel="Stylesheet" />\n <script>\n window.KIWIX_RESPONSE_TEMPLATE = "{{KIWIX_RESPONSE_TEMPLATE}}";\n window.KIWIX_RESPONSE_DATA = {{{KIWIX_RESPONSE_DATA}}};\n </script>\n </head>\n <body>\n <header>\n <img src="{{root}}/skin/500.svg?cacheid=32eb0f20"\n alt="{{500_img_text}}"\n aria-label="{{500_img_text}}"\n title="{{500_img_text}}">\n </header>\n <section class="intro">\n <h1>{{PAGE_HEADING}}</h1>\n <p>{{PAGE_TEXT}}</p>\n <p><code>{{url_path}}</code></p>\n </section>\n{{#error}}\n <section class="advice">\n <p>{{error}}</p>\n </section>\n{{/error}}\n </body>\n</html>\n";
|
||||
window.KIWIX_RESPONSE_DATA = { "500_img_text" : { "msgid" : "500-img-text", "params" : { } }, "PAGE_HEADING" : { "msgid" : "500-page-heading", "params" : { } }, "PAGE_TEXT" : { "msgid" : "500-page-text", "params" : { } }, "PAGE_TITLE" : { "msgid" : "500-page-title", "params" : { } }, "error" : ")RAWSTRINGLITERAL"
|
||||
+ // inject the error
|
||||
escapeJsString(error) // inject the error
|
||||
+ // inject the error
|
||||
R"RAWSTRINGLITERAL(", "root" : "/ROOT%23%3F", "url_path" : ")RAWSTRINGLITERAL"
|
||||
+ // inject the URL
|
||||
jsSafeUrl // inject the URL
|
||||
+ // inject the URL
|
||||
@ -1265,6 +1270,9 @@ std::string expectedSexy500ErrorHtml(const std::string& url)
|
||||
+ // inject the URL
|
||||
R"RAWSTRINGLITERAL(</code></p>
|
||||
</section>
|
||||
<section class="advice">
|
||||
<p>)RAWSTRINGLITERAL" + error + R"RAWSTRINGLITERAL(</p>
|
||||
</section>
|
||||
</body>
|
||||
</html>
|
||||
)RAWSTRINGLITERAL";
|
||||
@ -1272,17 +1280,28 @@ std::string expectedSexy500ErrorHtml(const std::string& url)
|
||||
|
||||
TEST_F(ServerTest, 500)
|
||||
{
|
||||
const std::vector<std::string> testUrls {
|
||||
"/ROOT%23%3F/content/poor/A/redirect_loop.html",
|
||||
"/ROOT%23%3F/content/poor/A/redirect_loop.html?userlang=test",
|
||||
struct TestData {
|
||||
const std::string url;
|
||||
const std::string error;
|
||||
};
|
||||
const TestData testData[] = {
|
||||
{
|
||||
"/ROOT%23%3F/content/poor/A/redirect_loop.html",
|
||||
"Entry redirect_loop.html is a redirect entry."
|
||||
},
|
||||
|
||||
{
|
||||
"/ROOT%23%3F/content/poor/A/redirect_loop.html?userlang=test",
|
||||
"Entry redirect_loop.html is a redirect entry."
|
||||
}
|
||||
};
|
||||
|
||||
for ( const auto& url : testUrls ) {
|
||||
const TestContext ctx{ {"url", url} };
|
||||
const auto r = zfs1_->GET(url.c_str());
|
||||
for ( const auto& td : testData ) {
|
||||
const TestContext ctx{ {"url", td.url} };
|
||||
const auto r = zfs1_->GET(td.url.c_str());
|
||||
EXPECT_EQ(r->status, 500) << ctx;
|
||||
EXPECT_EQ(r->get_header_value("Content-Type"), "text/html; charset=utf-8") << ctx;
|
||||
EXPECT_EQ(r->body, expectedSexy500ErrorHtml(url)) << ctx;
|
||||
EXPECT_EQ(r->body, expectedSexy500ErrorHtml(td.url, td.error)) << ctx;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user