Less confusing 404 errors from /random endpoint

Before this fix the /random endpoint could return a 404 Not Found page
saying

  The requested URL "/random" was not found on this server.

Error cases producing such a result were:

- `/random?content=NON-EXISTING-BOOK` (can happen when a server is
restarted or the library is reloaded and the current book is no longer
available).

- Failure of the libkiwix routine for picking a random article.

Now a proper message is shown for each of those cases.
This commit is contained in:
Veloman Yunkan 2021-12-11 20:54:16 +04:00 committed by Matthieu Gautier
parent d21879ebda
commit f7b853373c
3 changed files with 10 additions and 4 deletions

View File

@ -668,18 +668,20 @@ std::unique_ptr<Response> InternalServer::handle_random(const RequestContext& re
bookId = mp_nameMapper->getIdForName(bookName); bookId = mp_nameMapper->getIdForName(bookName);
archive = mp_library->getArchiveById(bookId); archive = mp_library->getArchiveById(bookId);
} catch (const std::out_of_range&) { } catch (const std::out_of_range&) {
return Response::build_404(*this, request, bookName, ""); // error handled by the archive == nullptr check below
} }
if (archive == nullptr) { if (archive == nullptr) {
return Response::build_404(*this, request, bookName, ""); const std::string error_details = "No such book: " + bookName;
return Response::build_404(*this, request, bookName, "", error_details);
} }
try { try {
auto entry = archive->getRandomEntry(); auto entry = archive->getRandomEntry();
return build_redirect(bookName, getFinalItem(*archive, entry)); return build_redirect(bookName, getFinalItem(*archive, entry));
} catch(zim::EntryNotFound& e) { } catch(zim::EntryNotFound& e) {
return Response::build_404(*this, request, bookName, ""); const std::string error_details = "Oops! Failed to pick a random article :(";
return Response::build_404(*this, request, bookName, getArchiveTitle(*archive), error_details);
} }
} }

View File

@ -86,7 +86,9 @@ std::unique_ptr<Response> Response::build_304(const InternalServer& server, cons
std::unique_ptr<Response> Response::build_404(const InternalServer& server, const RequestContext& request, const std::string& bookName, const std::string& bookTitle, const std::string& details) std::unique_ptr<Response> Response::build_404(const InternalServer& server, const RequestContext& request, const std::string& bookName, const std::string& bookTitle, const std::string& details)
{ {
MustacheData results; MustacheData results;
if ( request.get_url() != "/random" ) {
results.set("url", request.get_full_url()); results.set("url", request.get_full_url());
}
results.set("details", details); results.set("details", details);
auto response = ContentResponse::build(server, RESOURCE::templates::_404_html, results, "text/html"); auto response = ContentResponse::build(server, RESOURCE::templates::_404_html, results, "text/html");

View File

@ -6,9 +6,11 @@
</head> </head>
<body> <body>
<h1>Not Found</h1> <h1>Not Found</h1>
{{#url}}
<p> <p>
The requested URL "{{url}}" was not found on this server. The requested URL "{{url}}" was not found on this server.
</p> </p>
{{/url}}
{{#details}} {{#details}}
<p> <p>
{{{details}}} {{{details}}}