@@ -178,8 +178,8 @@ public Result authority(String id, String format) {
178
178
Format responseFormat = Accept .formatFor (format , request ().acceptedTypes ());
179
179
if (responseFormat == null || responseFormat == Accept .Format .JSON_LINES
180
180
|| format != null && format .contains (":" )) {
181
- return unsupportedMediaType (String . format ( "Unsupported for single resource: format=%s, accept=%s" , format ,
182
- request ().acceptedTypes ()));
181
+ return unsupportedMediaType (views . html . error . render ( id , String . format (
182
+ "Unsupported for single resource: format=%s, accept=%s" , format , request ().acceptedTypes () )));
183
183
}
184
184
try {
185
185
switch (responseFormat ) {
@@ -199,7 +199,7 @@ public Result authority(String id, String format) {
199
199
}
200
200
} catch (Exception e ) {
201
201
Logger .error ("Could not create response" , e );
202
- return internalServerError (e .getMessage ());
202
+ return internalServerError (views . html . error . render ( id , e .getMessage () ));
203
203
}
204
204
}
205
205
@@ -297,31 +297,37 @@ public Result search(String q, String filter, int from, int size, String format)
297
297
Format responseFormat = Accept .formatFor (format , request ().acceptedTypes ());
298
298
if (responseFormat == null || Stream .of (RdfFormat .values ()).map (RdfFormat ::getParam )
299
299
.anyMatch (f -> f .equals (responseFormat .queryParamString ))) {
300
- return unsupportedMediaType (
301
- String .format ("Unsupported for search: format=%s, accept=%s" , format , request ().acceptedTypes ()));
300
+ return unsupportedMediaType (views . html . error . render ( q ,
301
+ String .format ("Unsupported for search: format=%s, accept=%s" , format , request ().acceptedTypes ()))) ;
302
302
}
303
303
String queryString = (q == null || q .isEmpty ()) ? "*" : q ;
304
- SearchResponse response = index .query (queryString , filter , from , size );
305
- response ().setHeader ("Access-Control-Allow-Origin" , "*" );
306
- String [] formatAndConfig = format == null ? new String [] {} : format .split (":" );
307
- boolean returnSuggestions = formatAndConfig .length == 2 ;
308
- if (returnSuggestions ) {
309
- List <Map <String , Object >> hits = Arrays .asList (response .getHits ().getHits ()).stream ()
310
- .map (hit -> hit .getSource ()).collect (Collectors .toList ());
311
- return withCallback (toSuggestions (Json .toJson (hits ), formatAndConfig [1 ]));
312
- }
313
- switch (responseFormat ) {
314
- case HTML : {
315
- return htmlSearch (q , filter , from , size , responseFormat .queryParamString , response );
316
- }
317
- case JSON_LINES : {
318
- response ().setHeader ("Content-Disposition" ,
319
- String .format ("attachment; filename=\" lobid-gnd-bulk-%s.jsonl\" " , System .currentTimeMillis ()));
320
- return jsonLines (queryString , filter , response );
321
- }
322
- default : {
323
- return ok (returnAsJson (q , response )).as (config ("index.content" ));
324
- }
304
+ try {
305
+ SearchResponse response = index .query (queryString , filter , from , size );
306
+ response ().setHeader ("Access-Control-Allow-Origin" , "*" );
307
+ String [] formatAndConfig = format == null ? new String [] {} : format .split (":" );
308
+ boolean returnSuggestions = formatAndConfig .length == 2 ;
309
+ if (returnSuggestions ) {
310
+ List <Map <String , Object >> hits = Arrays .asList (response .getHits ().getHits ()).stream ()
311
+ .map (hit -> hit .getSource ()).collect (Collectors .toList ());
312
+ return withCallback (toSuggestions (Json .toJson (hits ), formatAndConfig [1 ]));
313
+ }
314
+ switch (responseFormat ) {
315
+ case HTML : {
316
+ return htmlSearch (q , filter , from , size , responseFormat .queryParamString , response );
317
+ }
318
+ case JSON_LINES : {
319
+ response ().setHeader ("Content-Disposition" ,
320
+ String .format ("attachment; filename=\" lobid-gnd-bulk-%s.jsonl\" " , System .currentTimeMillis ()));
321
+ return jsonLines (queryString , filter , response );
322
+ }
323
+ default : {
324
+ return ok (returnAsJson (q , response )).as (config ("index.content" ));
325
+ }
326
+ }
327
+ } catch (Throwable t ) {
328
+ String message = t .getMessage () + (t .getCause () != null ? ", cause: " + t .getCause ().getMessage () : "" );
329
+ Logger .error ("Error: {}" , message );
330
+ return internalServerError (views .html .error .render (q , "Error: " + message ));
325
331
}
326
332
}
327
333
@@ -361,7 +367,7 @@ public ByteString next() {
361
367
362
368
private Result htmlSearch (String q , String type , int from , int size , String format , SearchResponse response ) {
363
369
return ok (views .html .search .render (q , type , from , size , returnAsJson (q , response ),
364
- response .getHits ().getTotalHits ()));
370
+ response == null ? 0 : response .getHits ().getTotalHits ()));
365
371
}
366
372
367
373
private static Result withCallback (final String json ) {
@@ -434,6 +440,9 @@ public static String currentUri() {
434
440
}
435
441
436
442
private static String returnAsJson (String q , SearchResponse queryResponse ) {
443
+ if (queryResponse == null ) {
444
+ return Json .newObject ().toString ();
445
+ }
437
446
List <Map <String , Object >> hits = Arrays .asList (queryResponse .getHits ().getHits ()).stream ()
438
447
.map (hit -> hit .getSource ()).collect (Collectors .toList ());
439
448
ObjectNode object = Json .newObject ();
0 commit comments