Fully internationalized 400, 404 & 500 error pages

This commit is contained in:
Veloman Yunkan 2022-01-23 23:24:09 +04:00 committed by Matthieu Gautier
parent d2c864b010
commit fbd23a8329
7 changed files with 42 additions and 17 deletions

View File

@ -628,8 +628,8 @@ std::unique_ptr<Response> InternalServer::handle_search(const RequestContext& re
// Searcher->search will throw a runtime error if there is no valid xapian database to do the search.
// (in case of zim file not containing a index)
return HTTPErrorHtmlResponse(*this, request, MHD_HTTP_NOT_FOUND,
"Fulltext search unavailable",
"Not Found",
"fulltext-search-unavailable",
"404-page-heading",
m_root + "/skin/search_results.css")
+ noSearchResultsMsg()
+ TaskbarInfo(searchInfo.bookName, archive.get());

View File

@ -87,6 +87,11 @@ std::unique_ptr<Response> Response::build_304(const InternalServer& server, cons
const UrlNotFoundMsg urlNotFoundMsg;
const InvalidUrlMsg invalidUrlMsg;
std::string ContentResponseBlueprint::getMessage(const std::string& msgId) const
{
return getTranslatedString(m_request.get_user_language(), msgId);
}
std::unique_ptr<ContentResponse> ContentResponseBlueprint::generateResponseObject() const
{
auto r = ContentResponse::build(m_server, m_template, m_data, m_mimeType);
@ -100,8 +105,8 @@ std::unique_ptr<ContentResponse> ContentResponseBlueprint::generateResponseObjec
HTTPErrorHtmlResponse::HTTPErrorHtmlResponse(const InternalServer& server,
const RequestContext& request,
int httpStatusCode,
const std::string& pageTitleMsg,
const std::string& headingMsg,
const std::string& pageTitleMsgId,
const std::string& headingMsgId,
const std::string& cssUrl)
: ContentResponseBlueprint(&server,
&request,
@ -112,8 +117,8 @@ HTTPErrorHtmlResponse::HTTPErrorHtmlResponse(const InternalServer& server,
kainjow::mustache::list emptyList;
this->m_data = kainjow::mustache::object{
{"CSS_URL", onlyAsNonEmptyMustacheValue(cssUrl) },
{"PAGE_TITLE", pageTitleMsg},
{"PAGE_HEADING", headingMsg},
{"PAGE_TITLE", getMessage(pageTitleMsgId)},
{"PAGE_HEADING", getMessage(headingMsgId)},
{"details", emptyList}
};
}
@ -123,8 +128,8 @@ HTTP404HtmlResponse::HTTP404HtmlResponse(const InternalServer& server,
: HTTPErrorHtmlResponse(server,
request,
MHD_HTTP_NOT_FOUND,
"Content not found",
"Not Found")
"404-page-title",
"404-page-heading")
{
}
@ -151,8 +156,8 @@ HTTP400HtmlResponse::HTTP400HtmlResponse(const InternalServer& server,
: HTTPErrorHtmlResponse(server,
request,
MHD_HTTP_BAD_REQUEST,
"Invalid request",
"Invalid request")
"400-page-title",
"400-page-heading")
{
}
@ -172,8 +177,8 @@ HTTP500HtmlResponse::HTTP500HtmlResponse(const InternalServer& server,
: HTTPErrorHtmlResponse(server,
request,
MHD_HTTP_INTERNAL_SERVER_ERROR,
"Internal Server Error",
"Internal Server Error")
"500-page-title",
"500-page-heading")
{
// operator+() is a state-modifying operator (akin to operator+=)
*this + "An internal server error occured. We are sorry about that :/";

View File

@ -167,6 +167,7 @@ public: // functions
ContentResponseBlueprint& operator+(const TaskbarInfo& taskbarInfo);
protected: // functions
std::string getMessage(const std::string& msgId) const;
virtual std::unique_ptr<ContentResponse> generateResponseObject() const;
public: //data
@ -184,8 +185,8 @@ struct HTTPErrorHtmlResponse : ContentResponseBlueprint
HTTPErrorHtmlResponse(const InternalServer& server,
const RequestContext& request,
int httpStatusCode,
const std::string& pageTitleMsg,
const std::string& headingMsg,
const std::string& pageTitleMsgId,
const std::string& headingMsgId,
const std::string& cssUrl = "");
using ContentResponseBlueprint::operator+;

View File

@ -11,4 +11,11 @@
, "random-article-failure" : "Oops! Failed to pick a random article :("
, "invalid-raw-data-type" : "{{DATATYPE}} is not a valid request for raw content."
, "raw-entry-not-found" : "Cannot find {{DATATYPE}} entry {{ENTRY}}"
, "400-page-title" : "Invalid request"
, "400-page-heading" : "Invalid request"
, "404-page-title" : "Content not found"
, "404-page-heading" : "Not Found"
, "500-page-title" : "Internal Server Error"
, "500-page-heading" : "Internal Server Error"
, "fulltext-search-unavailable" : "Fulltext search unavailable"
}

View File

@ -7,4 +7,6 @@
"suggest-full-text-search": "որոնել '{{{SEARCH_TERMS}}}'..."
, "no-such-book": "Գիրքը բացակայում է՝ {{BOOK_NAME}}"
, "url-not-found" : "Սխալ հասցե՝ {{url}}"
, "404-page-title" : "Սխալ հասցե"
, "404-page-heading" : "Սխալ հասցե"
}

View File

@ -12,4 +12,11 @@
, "random-article-failure" : "Failure of the random article selection procedure"
, "invalid-raw-data-type" : "Invalid DATATYPE was used with the /raw endpoint (/raw/<book>/DATATYPE/...); allowed values are 'meta' and 'content'"
, "raw-entry-not-found" : "Entry requested via the /raw endpoint was not found"
, "400-page-title" : "Title of the 400 error page"
, "400-page-heading" : "Heading of the 400 error page"
, "404-page-title" : "Title of the 404 error page"
, "404-page-heading" : "Heading of the 404 error page"
, "500-page-title" : "Title of the 500 error page"
, "500-page-heading" : "Heading of the 500 error page"
, "fulltext-search-unavailable" : "Title of the error page returned when search is attempted in a book without fulltext search database"
}

View File

@ -572,8 +572,9 @@ TEST_F(ServerTest, 404WithBodyTesting)
)" },
{ /* url */ "/ROOT/random?content=non-existent-book&userlang=hy",
expected_page_title=="Սխալ հասցե" &&
expected_body==R"(
<h1>Not Found</h1>
<h1>Սխալ հասցե</h1>
<p>
Գիրքը բացակայում է՝ non-existent-book
</p>
@ -596,8 +597,9 @@ TEST_F(ServerTest, 404WithBodyTesting)
)" },
{ /* url */ "/ROOT/catalog/?userlang=hy",
expected_page_title=="Սխալ հասցե" &&
expected_body==R"(
<h1>Not Found</h1>
<h1>Սխալ հասցե</h1>
<p>
Սխալ հասցե՝ /ROOT/catalog/
</p>
@ -612,8 +614,9 @@ TEST_F(ServerTest, 404WithBodyTesting)
)" },
{ /* url */ "/ROOT/catalog/invalid_endpoint?userlang=hy",
expected_page_title=="Սխալ հասցե" &&
expected_body==R"(
<h1>Not Found</h1>
<h1>Սխալ հասցե</h1>
<p>
Սխալ հասցե՝ /ROOT/catalog/invalid_endpoint
</p>