Introduce Error exception to do i18n

This commit is contained in:
Matthieu Gautier 2022-04-21 15:41:14 +02:00
parent c72132054d
commit f0065fdd6f
4 changed files with 39 additions and 13 deletions

View File

@ -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<Response> 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();
}
}

View File

@ -6,10 +6,13 @@
"name":"English",
"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 <a href=\"{{{SEARCH_URL}}}\">{{PATTERN}}</a>"
, "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"

View File

@ -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 languages 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/<book>/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",

View File

@ -919,7 +919,7 @@ TEST_F(ServerTest, 400WithBodyTesting)
The requested URL "/ROOT/search?content=non-existing-book&pattern=asdfqwerty" is not a valid request.
</p>
<p>
The requested book doesn't exist.
No such book: non-existing-book
</p>
)" },
{ /* url */ "/ROOT/search?content=non-existing-book&pattern=a\"<script foo>",
@ -929,7 +929,7 @@ TEST_F(ServerTest, 400WithBodyTesting)
The requested URL "/ROOT/search?content=non-existing-book&pattern=a"&lt;script foo&gt;" is not a valid request.
</p>
<p>
The requested book doesn't exist.
No such book: non-existing-book
</p>
)" },
// There is a flaw in our way to handle query string, we cannot differenciate