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 #156
  • Loading branch information
fsteeg committed Sep 20, 2018
1 parent aa879bc commit f92d5c5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 35 deletions.
58 changes: 32 additions & 26 deletions app/controllers/HomeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ public Result authority(String id, String format) {
Format responseFormat = Accept.formatFor(format, request().acceptedTypes());
if (responseFormat == null || responseFormat == Accept.Format.JSON_LINES
|| format != null && format.contains(":")) {
return unsupportedMediaType(String.format("Unsupported for single resource: format=%s, accept=%s", format,
request().acceptedTypes()));
return unsupportedMediaType(views.html.error.render(id, String.format(
"Unsupported for single resource: format=%s, accept=%s", format, request().acceptedTypes())));
}
try {
switch (responseFormat) {
Expand All @@ -199,7 +199,7 @@ public Result authority(String id, String format) {
}
} catch (Exception e) {
Logger.error("Could not create response", e);
return internalServerError(e.getMessage());
return internalServerError(views.html.error.render(id, e.getMessage()));
}
}

Expand Down Expand Up @@ -297,31 +297,37 @@ public Result search(String q, String filter, int from, int size, String format)
Format responseFormat = Accept.formatFor(format, request().acceptedTypes());
if (responseFormat == null || Stream.of(RdfFormat.values()).map(RdfFormat::getParam)
.anyMatch(f -> f.equals(responseFormat.queryParamString))) {
return unsupportedMediaType(
String.format("Unsupported for search: format=%s, accept=%s", format, request().acceptedTypes()));
return unsupportedMediaType(views.html.error.render(q,
String.format("Unsupported for search: format=%s, accept=%s", format, request().acceptedTypes())));
}
String queryString = (q == null || q.isEmpty()) ? "*" : q;
SearchResponse response = index.query(queryString, filter, from, size);
response().setHeader("Access-Control-Allow-Origin", "*");
String[] formatAndConfig = format == null ? new String[] {} : format.split(":");
boolean returnSuggestions = formatAndConfig.length == 2;
if (returnSuggestions) {
List<Map<String, Object>> hits = Arrays.asList(response.getHits().getHits()).stream()
.map(hit -> hit.getSource()).collect(Collectors.toList());
return withCallback(toSuggestions(Json.toJson(hits), formatAndConfig[1]));
}
switch (responseFormat) {
case HTML: {
return htmlSearch(q, filter, from, size, responseFormat.queryParamString, response);
}
case JSON_LINES: {
response().setHeader("Content-Disposition",
String.format("attachment; filename=\"lobid-gnd-bulk-%s.jsonl\"", System.currentTimeMillis()));
return jsonLines(queryString, filter, response);
}
default: {
return ok(returnAsJson(q, response)).as(config("index.content"));
}
try {
SearchResponse response = index.query(queryString, filter, from, size);
response().setHeader("Access-Control-Allow-Origin", "*");
String[] formatAndConfig = format == null ? new String[] {} : format.split(":");
boolean returnSuggestions = formatAndConfig.length == 2;
if (returnSuggestions) {
List<Map<String, Object>> hits = Arrays.asList(response.getHits().getHits()).stream()
.map(hit -> hit.getSource()).collect(Collectors.toList());
return withCallback(toSuggestions(Json.toJson(hits), formatAndConfig[1]));
}
switch (responseFormat) {
case HTML: {
return htmlSearch(q, filter, from, size, responseFormat.queryParamString, response);
}
case JSON_LINES: {
response().setHeader("Content-Disposition",
String.format("attachment; filename=\"lobid-gnd-bulk-%s.jsonl\"", System.currentTimeMillis()));
return jsonLines(queryString, filter, response);
}
default: {
return ok(returnAsJson(q, response)).as(config("index.content"));
}
}
} 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
12 changes: 3 additions & 9 deletions app/modules/IndexComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,15 +259,9 @@ public SearchResponse query(String q, String filter, int from, int size) {
for (String a : HomeController.AGGREGATIONS) {
requestBuilder.addAggregation(AggregationBuilders.terms(a).field(a).size(1000));
}
try {
Logger.debug("Search request: {}", requestBuilder);
SearchResponse response = requestBuilder.get();
return response;
} catch (Throwable t) {
Logger.error("Could not execute request: {}, cause: {}", t.getMessage(), t.getCause().getMessage());
Logger.debug("Could not execute request", t);
return null;
}
Logger.debug("Search request: {}", requestBuilder);
SearchResponse response = requestBuilder.get();
return response;
}

@Override
Expand Down
10 changes: 10 additions & 0 deletions app/views/error.scala.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@* Copyright 2018 Fabian Steeg, hbz. Licensed under the GPLv2 *@

@(q: String, message: String)

@main(q, "Error") {
<div class="panel panel-danger footer">
<div class='panel-heading'>Fehler</div>
<div class='panel-body'>@message</div>
</div>
}

0 comments on commit f92d5c5

Please sign in to comment.