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. // 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) // (in case of zim file not containing a index)
return HTTPErrorHtmlResponse(*this, request, MHD_HTTP_NOT_FOUND, return HTTPErrorHtmlResponse(*this, request, MHD_HTTP_NOT_FOUND,
"Fulltext search unavailable", "fulltext-search-unavailable",
"Not Found", "404-page-heading",
m_root + "/skin/search_results.css") m_root + "/skin/search_results.css")
+ noSearchResultsMsg() + noSearchResultsMsg()
+ TaskbarInfo(searchInfo.bookName, archive.get()); + 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 UrlNotFoundMsg urlNotFoundMsg;
const InvalidUrlMsg invalidUrlMsg; 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 std::unique_ptr<ContentResponse> ContentResponseBlueprint::generateResponseObject() const
{ {
auto r = ContentResponse::build(m_server, m_template, m_data, m_mimeType); 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, HTTPErrorHtmlResponse::HTTPErrorHtmlResponse(const InternalServer& server,
const RequestContext& request, const RequestContext& request,
int httpStatusCode, int httpStatusCode,
const std::string& pageTitleMsg, const std::string& pageTitleMsgId,
const std::string& headingMsg, const std::string& headingMsgId,
const std::string& cssUrl) const std::string& cssUrl)
: ContentResponseBlueprint(&server, : ContentResponseBlueprint(&server,
&request, &request,
@ -112,8 +117,8 @@ HTTPErrorHtmlResponse::HTTPErrorHtmlResponse(const InternalServer& server,
kainjow::mustache::list emptyList; kainjow::mustache::list emptyList;
this->m_data = kainjow::mustache::object{ this->m_data = kainjow::mustache::object{
{"CSS_URL", onlyAsNonEmptyMustacheValue(cssUrl) }, {"CSS_URL", onlyAsNonEmptyMustacheValue(cssUrl) },
{"PAGE_TITLE", pageTitleMsg}, {"PAGE_TITLE", getMessage(pageTitleMsgId)},
{"PAGE_HEADING", headingMsg}, {"PAGE_HEADING", getMessage(headingMsgId)},
{"details", emptyList} {"details", emptyList}
}; };
} }
@ -123,8 +128,8 @@ HTTP404HtmlResponse::HTTP404HtmlResponse(const InternalServer& server,
: HTTPErrorHtmlResponse(server, : HTTPErrorHtmlResponse(server,
request, request,
MHD_HTTP_NOT_FOUND, MHD_HTTP_NOT_FOUND,
"Content not found", "404-page-title",
"Not Found") "404-page-heading")
{ {
} }
@ -151,8 +156,8 @@ HTTP400HtmlResponse::HTTP400HtmlResponse(const InternalServer& server,
: HTTPErrorHtmlResponse(server, : HTTPErrorHtmlResponse(server,
request, request,
MHD_HTTP_BAD_REQUEST, MHD_HTTP_BAD_REQUEST,
"Invalid request", "400-page-title",
"Invalid request") "400-page-heading")
{ {
} }
@ -172,8 +177,8 @@ HTTP500HtmlResponse::HTTP500HtmlResponse(const InternalServer& server,
: HTTPErrorHtmlResponse(server, : HTTPErrorHtmlResponse(server,
request, request,
MHD_HTTP_INTERNAL_SERVER_ERROR, MHD_HTTP_INTERNAL_SERVER_ERROR,
"Internal Server Error", "500-page-title",
"Internal Server Error") "500-page-heading")
{ {
// operator+() is a state-modifying operator (akin to operator+=) // operator+() is a state-modifying operator (akin to operator+=)
*this + "An internal server error occured. We are sorry about that :/"; *this + "An internal server error occured. We are sorry about that :/";

View File

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

View File

@ -11,4 +11,11 @@
, "random-article-failure" : "Oops! Failed to pick a random article :(" , "random-article-failure" : "Oops! Failed to pick a random article :("
, "invalid-raw-data-type" : "{{DATATYPE}} is not a valid request for raw content." , "invalid-raw-data-type" : "{{DATATYPE}} is not a valid request for raw content."
, "raw-entry-not-found" : "Cannot find {{DATATYPE}} entry {{ENTRY}}" , "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}}}'..." "suggest-full-text-search": "որոնել '{{{SEARCH_TERMS}}}'..."
, "no-such-book": "Գիրքը բացակայում է՝ {{BOOK_NAME}}" , "no-such-book": "Գիրքը բացակայում է՝ {{BOOK_NAME}}"
, "url-not-found" : "Սխալ հասցե՝ {{url}}" , "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" , "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'" , "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" , "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", { /* url */ "/ROOT/random?content=non-existent-book&userlang=hy",
expected_page_title=="Սխալ հասցե" &&
expected_body==R"( expected_body==R"(
<h1>Not Found</h1> <h1>Սխալ հասցե</h1>
<p> <p>
Գիրքը բացակայում է՝ non-existent-book Գիրքը բացակայում է՝ non-existent-book
</p> </p>
@ -596,8 +597,9 @@ TEST_F(ServerTest, 404WithBodyTesting)
)" }, )" },
{ /* url */ "/ROOT/catalog/?userlang=hy", { /* url */ "/ROOT/catalog/?userlang=hy",
expected_page_title=="Սխալ հասցե" &&
expected_body==R"( expected_body==R"(
<h1>Not Found</h1> <h1>Սխալ հասցե</h1>
<p> <p>
Սխալ հասցե՝ /ROOT/catalog/ Սխալ հասցե՝ /ROOT/catalog/
</p> </p>
@ -612,8 +614,9 @@ TEST_F(ServerTest, 404WithBodyTesting)
)" }, )" },
{ /* url */ "/ROOT/catalog/invalid_endpoint?userlang=hy", { /* url */ "/ROOT/catalog/invalid_endpoint?userlang=hy",
expected_page_title=="Սխալ հասցե" &&
expected_body==R"( expected_body==R"(
<h1>Not Found</h1> <h1>Սխալ հասցե</h1>
<p> <p>
Սխալ հասցե՝ /ROOT/catalog/invalid_endpoint Սխալ հասցե՝ /ROOT/catalog/invalid_endpoint
</p> </p>