Less confusing 404 errors from /meta endpoint

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

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

Error cases producing such a result were:

- `/meta?content=NON-EXISTING-BOOK&name=metaname`

- `/meta?content=book&name=BAD-META-NAME`

Now a proper message is shown for each of those cases.

This fix is being done just for consistency (the /meta endpoint is not
a user-facing one and the scripts don't bother about error texts).
This commit is contained in:
Veloman Yunkan 2021-12-11 21:45:12 +04:00 committed by Matthieu Gautier
parent d8c525289b
commit 20b5a2b971
1 changed files with 5 additions and 4 deletions

View File

@ -386,23 +386,23 @@ std::unique_ptr<Response> InternalServer::handle_meta(const RequestContext& requ
{
std::string bookName;
std::string bookId;
std::string meta_name;
std::shared_ptr<zim::Archive> archive;
try {
bookName = request.get_argument("content");
bookId = mp_nameMapper->getIdForName(bookName);
meta_name = request.get_argument("name");
archive = mp_library->getArchiveById(bookId);
} catch (const std::out_of_range& e) {
// error handled by the archive == nullptr check below
}
if (archive == nullptr) {
return Response::build_404(*this, request.get_full_url(), bookName, "");
const std::string error_details = "No such book: " + bookName;
return Response::build_404(*this, "", bookName, "", error_details);
}
std::string content;
std::string mimeType = "text";
const auto meta_name = request.get_optional_param("name", std::string());
if (meta_name == "title") {
content = getArchiveTitle(*archive);
@ -425,7 +425,8 @@ std::unique_ptr<Response> InternalServer::handle_meta(const RequestContext& requ
} else if (const unsigned illustrationSize = parseIllustration(meta_name)) {
getArchiveFavicon(*archive, illustrationSize, content, mimeType);
} else {
return Response::build_404(*this, request.get_full_url(), bookName, "");
const std::string error_details = "No such metadata item: " + meta_name;
return Response::build_404(*this, "", bookName, "", error_details);
}
auto response = ContentResponse::build(*this, content, mimeType);