diff --git a/src/server/internalServer.cpp b/src/server/internalServer.cpp index 257ef5312..a375940ad 100644 --- a/src/server/internalServer.cpp +++ b/src/server/internalServer.cpp @@ -440,38 +440,26 @@ std::unique_ptr InternalServer::handle_suggest(const RequestContext& r printf("** running handle_suggest\n"); } - std::string content; - std::string mimeType; - std::string bookName; - std::string bookId; - std::string queryString; std::shared_ptr archive; try { bookName = request.get_argument("content"); - bookId = mp_nameMapper->getIdForName(bookName); - queryString = request.get_argument("term"); + const std::string bookId = mp_nameMapper->getIdForName(bookName); archive = mp_library->getArchiveById(bookId); } catch (const std::out_of_range&) { - return Response::build_404(*this, "", bookName, ""); - } - - auto start = 0; - try { - start = request.get_argument("start"); - } catch (const std::exception&) {} - - unsigned int count = 10; - try { - count = request.get_argument("count"); - } catch (const std::exception&) {} - - if (count == 0) { - count = 10; + // error handled by the archive == nullptr check below } if (archive == nullptr) { - return Response::build_404(*this, "", bookName, ""); + const std::string error_details = "No such book: " + bookName; + return Response::build_404(*this, "", bookName, "", error_details); + } + + const auto queryString = request.get_optional_param("term", std::string()); + const auto start = request.get_optional_param("start", 0); + unsigned int count = request.get_optional_param("count", 10); + if (count == 0) { + count = 10; } if (m_verbose.load()) { diff --git a/test/server.cpp b/test/server.cpp index afffee224..49cfab5a1 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -179,6 +179,7 @@ const ResourceCollection resources200Compressible{ { NO_ETAG, "/search?content=zimfile&pattern=a" }, + { NO_ETAG, "/suggest?content=zimfile" }, { NO_ETAG, "/suggest?content=zimfile&term=ray" }, { NO_ETAG, "/catch/external?source=www.example.com" }, @@ -282,7 +283,6 @@ const char* urls404[] = { "/random?content=non-existent-book", "/search", "/suggest", - "/suggest?content=zimfile", "/suggest?content=non-existent-book&term=abcd", "/catch/external", "/zimfile/A/non-existent-article",