(search) Improve error handling in search parameters parsing

The code now intercepts and deals with potential exceptions during the parsing of search parameters. This is in response to constant bad requests from bots which were cluttering the logs. A catch clause is added that suppresses these errors and redirects to the base URL.
This commit is contained in:
Viktor Lofgren 2023-12-16 18:42:13 +01:00
parent e13fa25e11
commit d715b1f9ca

View File

@ -52,15 +52,23 @@ public class SearchQueryService {
}
private SearchParameters parseParameters(Request request) {
final String queryParam = request.queryParams("query");
try {
final String queryParam = request.queryParams("query");
if (null == queryParam || queryParam.isBlank()) {
throw new RedirectException(websiteUrl.url());
}
return new SearchParameters(queryParam.trim(),
SearchProfile.getSearchProfile(request.queryParams("profile")),
SearchJsParameter.parse(request.queryParams("js")),
SearchAdtechParameter.parse(request.queryParams("adtech")));
}
catch (Exception ex) {
// Bots keep sending bad requests, suppress the error otherwise it will
// fill up the logs.
if (null == queryParam || queryParam.isBlank()) {
throw new RedirectException(websiteUrl.url());
}
return new SearchParameters(queryParam.trim(),
SearchProfile.getSearchProfile(request.queryParams("profile")),
SearchJsParameter.parse(request.queryParams("js")),
SearchAdtechParameter.parse(request.queryParams("adtech")));
}
}