Skip to content

Commit

Permalink
Return proper error code, embed message in error template
Browse files Browse the repository at this point in the history
See #414
  • Loading branch information
fsteeg committed Sep 20, 2018
1 parent 593bead commit 4a47650
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 14 deletions.
13 changes: 7 additions & 6 deletions app/controllers/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,13 @@ location, from, size, responseFormat, lang().code(),
Logger.debug("Caching search result for request: {}", cacheKey);
Cache.set(cacheKey, searchResult, ONE_DAY);
return searchResult;
} catch (IllegalArgumentException x) {
Logger.warn("Bad request: ", x.getMessage());
return badRequest("Bad request: " + x.getMessage());
} catch (Throwable t) {
String message = t.getMessage()
+ (t.getCause() != null ? ", cause: " + t.getCause().getMessage()
: "");
Logger.error("Error: {}", message);
return internalServerError(
views.html.error.render(q, "Error: " + message));
}
}

Expand Down Expand Up @@ -614,9 +618,6 @@ static String[] defaultAggregations() {
}

private static String returnAsJson(SearchResponse queryResponse) {
if (queryResponse == null) {
return Json.newObject().toString();
}
List<Map<String, Object>> hits =
Arrays.asList(queryResponse.getHits().hits()).stream()
.map(hit -> hit.getSource()).collect(Collectors.toList());
Expand Down
9 changes: 1 addition & 8 deletions app/controllers/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,7 @@ static SearchResponse executeQuery(int from, int size, QueryBuilder query,
if (!aggregations.isEmpty()) {
searchRequest = withAggregations(searchRequest, aggregations.split(","));
}
try {
return searchRequest.execute().actionGet();
} catch (Throwable t) {
Logger.error("Could not execute request: {}, cause: {}", t.getMessage(),
t.getCause().getMessage());
Logger.debug("Could not execute request", t);
return null;
}
return searchRequest.execute().actionGet();
}

private static QueryBuilder preprocess(QueryBuilder query) {
Expand Down
12 changes: 12 additions & 0 deletions app/views/error.scala.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@* Copyright 2018 Fabian Steeg, hbz. Licensed under the GPLv2 *@

@(q: String, message: String)

@import play.i18n._

@main(q) {
<div class="panel panel-danger footer">
<div class='panel-heading'>@Messages.get("error")</div>
<div class='panel-body'>@message</div>
</div>
}
2 changes: 2 additions & 0 deletions conf/messages.de
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,5 @@ dataset.distribution.documentation = Dokumentation
dataset.distribution.url = URL
dataset.distribution.license = Lizenz
dataset.distribution.media = Medientypen

error = Fehler
2 changes: 2 additions & 0 deletions conf/messages.en
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,5 @@ dataset.distribution.documentation = Documentation
dataset.distribution.url = URL
dataset.distribution.license = License
dataset.distribution.media = Media types

error = Error

0 comments on commit 4a47650

Please sign in to comment.