From f0065fdd6fde0d8dbdefed4ac657baedfe41c314 Mon Sep 17 00:00:00 2001
From: Matthieu Gautier
Date: Thu, 21 Apr 2022 15:41:14 +0200
Subject: [PATCH] Introduce Error exception to do i18n
---
src/server/internalServer.cpp | 35 +++++++++++++++++++++++++++--------
static/i18n/en.json | 7 +++++--
static/i18n/qqq.json | 6 +++++-
test/server.cpp | 4 ++--
4 files changed, 39 insertions(+), 13 deletions(-)
diff --git a/src/server/internalServer.cpp b/src/server/internalServer.cpp
index 6082bdbee..b667d47b9 100644
--- a/src/server/internalServer.cpp
+++ b/src/server/internalServer.cpp
@@ -158,6 +158,11 @@ ParameterizedMessage invalidRawAccessMsg(const std::string& dt)
return ParameterizedMessage("invalid-raw-data-type", { {"DATATYPE", dt} });
}
+ParameterizedMessage noValueForArgMsg(const std::string& argument)
+{
+ return ParameterizedMessage("no-value-for-arg", { {"ARGUMENT", argument} });
+}
+
ParameterizedMessage rawEntryNotFoundMsg(const std::string& dt, const std::string& entry)
{
return ParameterizedMessage("raw-entry-not-found",
@@ -174,6 +179,20 @@ ParameterizedMessage nonParameterizedMessage(const std::string& msgId)
return ParameterizedMessage(msgId, noParams);
}
+struct Error : public std::runtime_error {
+ explicit Error(const ParameterizedMessage& message)
+ : std::runtime_error("Error while handling request"),
+ _message(message)
+ {}
+
+ const ParameterizedMessage& message() const
+ {
+ return _message;
+ }
+
+ const ParameterizedMessage _message;
+};
+
} // unnamed namespace
Library::BookIdSet InternalServer::selectBooks(const RequestContext& request) const
@@ -184,7 +203,7 @@ Library::BookIdSet InternalServer::selectBooks(const RequestContext& request) co
try {
return {mp_nameMapper->getIdForName(bookName)};
} catch (const std::out_of_range&) {
- throw std::invalid_argument("The requested book doesn't exist.");
+ throw Error(noSuchBookErrorMsg(bookName));
}
} catch(const std::out_of_range&) {
// We've catch the out_of_range of get_argument
@@ -195,7 +214,7 @@ Library::BookIdSet InternalServer::selectBooks(const RequestContext& request) co
try {
auto id_vec = request.get_arguments("books.id");
if (id_vec.empty()) {
- throw std::invalid_argument("You must provide a value for the id.");
+ throw Error(noValueForArgMsg("books.id"));
}
return Library::BookIdSet(id_vec.begin(), id_vec.end());
} catch(const std::out_of_range&) {}
@@ -204,14 +223,14 @@ Library::BookIdSet InternalServer::selectBooks(const RequestContext& request) co
try {
auto name_vec = request.get_arguments("books.name");
if (name_vec.empty()) {
- throw std::invalid_argument("You must provide a value for the name.");
+ throw Error(noValueForArgMsg("books.name"));
}
Library::BookIdSet bookIds;
for(const auto& bookName: name_vec) {
try {
bookIds.insert(mp_nameMapper->getIdForName(bookName));
} catch(const std::out_of_range&) {
- throw std::invalid_argument("The requested book doesn't exist.");
+ throw Error(noSuchBookErrorMsg(bookName));
}
}
return bookIds;
@@ -221,7 +240,7 @@ Library::BookIdSet InternalServer::selectBooks(const RequestContext& request) co
Filter filter = get_search_filter(request, "books.filter.");
auto id_vec = mp_library->filter(filter);
if (id_vec.empty()) {
- throw std::invalid_argument("No books found.");
+ throw Error(nonParameterizedMessage("no-book-found"));
}
return Library::BookIdSet(id_vec.begin(), id_vec.end());
}
@@ -242,7 +261,7 @@ SearchInfo InternalServer::getSearchInfo(const RequestContext& request) const
catch(const std::invalid_argument&) {}
if (!geoQuery && pattern.empty()) {
- throw std::invalid_argument("No query provided.");
+ throw Error(nonParameterizedMessage("no-query"));
}
return SearchInfo(pattern, geoQuery, bookIds);
@@ -717,10 +736,10 @@ std::unique_ptr InternalServer::handle_search(const RequestContext& re
response->set_taskbar(bookName, mp_library->getArchiveById(bookId).get());
}
return std::move(response);
- } catch (const std::invalid_argument& e) {
+ } catch (const Error& e) {
return HTTP400HtmlResponse(*this, request)
+ invalidUrlMsg
- + std::string(e.what());
+ + e.message();
}
}
diff --git a/static/i18n/en.json b/static/i18n/en.json
index 183f8b9f4..c67e190c4 100644
--- a/static/i18n/en.json
+++ b/static/i18n/en.json
@@ -4,12 +4,15 @@
]
},
"name":"English",
- "suggest-full-text-search": "containing '{{{SEARCH_TERMS}}}'..."
- , "no-such-book": "No such book: {{BOOK_NAME}}"
+ "suggest-full-text-search" : "containing '{{{SEARCH_TERMS}}}'..."
+ , "no-such-book" : "No such book: {{BOOK_NAME}}"
+ , "no-book-found" : "No book matches selection criteria"
, "url-not-found" : "The requested URL \"{{url}}\" was not found on this server."
, "suggest-search" : "Make a full text search for {{PATTERN}}"
, "random-article-failure" : "Oops! Failed to pick a random article :("
, "invalid-raw-data-type" : "{{DATATYPE}} is not a valid request for raw content."
+ , "no-value-for-arg": "No value provided for argument {{ARGUMENT}}"
+ , "no-query" : "No query provided."
, "raw-entry-not-found" : "Cannot find {{DATATYPE}} entry {{ENTRY}}"
, "400-page-title" : "Invalid request"
, "400-page-heading" : "Invalid request"
diff --git a/static/i18n/qqq.json b/static/i18n/qqq.json
index edebb31dc..6a3f10caa 100644
--- a/static/i18n/qqq.json
+++ b/static/i18n/qqq.json
@@ -2,16 +2,20 @@
"@metadata": {
"authors": [
"Veloman Yunkan",
- "Verdy p"
+ "Verdy p",
+ "Matthieu Gautier"
]
},
"name": "{{Doc-important|Don't write \"English\" in your language!}}\n\n'''Write the name of ''your'' language in its native script.'''\n\nCurrent language to which the string is being translated to.\n\nFor example, write \"français\" when translating to French, or \"Deutsch\" when translating to German.\n\n'''Important:''' Do not use your language’s word for “English”. Use the word that your language uses to refer to itself. If you translate this message to mean “English” in your language, your change will be reverted.",
"suggest-full-text-search": "Text appearing in the suggestion list that, when selected, runs a full text search instead of the title search",
"no-such-book": "Error text when the requested book is not found in the library",
"url-not-found": "Error text about wrong URL for an HTTP 404 error",
+ "no-book-found": "Error text when no book matches the selection criteria",
"suggest-search": "Suggest a search when the URL points to a non existing article",
"random-article-failure": "Failure of the random article selection procedure",
"invalid-raw-data-type": "Invalid DATATYPE was used with the /raw endpoint (/raw//DATATYPE/...); allowed values are 'meta' and 'content'",
+ "no-value-for-arg" : "Error text when no value has been provided for ARGUMENT in the request's query string",
+ "no-query" : "Error text when no query has been provided for fulltext search",
"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",
diff --git a/test/server.cpp b/test/server.cpp
index 8b4e6b48b..128ed59ef 100644
--- a/test/server.cpp
+++ b/test/server.cpp
@@ -919,7 +919,7 @@ TEST_F(ServerTest, 400WithBodyTesting)
The requested URL "/ROOT/search?content=non-existing-book&pattern=asdfqwerty" is not a valid request.
- The requested book doesn't exist.
+ No such book: non-existing-book
)" },
{ /* url */ "/ROOT/search?content=non-existing-book&pattern=a\"