Skip to content

Commit b13741c

Browse files
committed
[HSEARCH] hsearch-elasticsearch-wikipedia: format REST endpoint errors as JSON
1 parent 2301fd5 commit b13741c

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

hibernate-search/hsearch-elasticsearch-wikipedia/src/main/java/org/hibernate/search/demos/wikipedia/endpoint/config/JerseyConfig.java

+4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
import org.glassfish.jersey.server.ResourceConfig;
44
import org.hibernate.search.demos.wikipedia.endpoint.AdminEndpoint;
55
import org.hibernate.search.demos.wikipedia.endpoint.PageEndpoint;
6+
import org.hibernate.search.demos.wikipedia.endpoint.error.ToJsonExceptionMapper;
7+
68
import org.springframework.stereotype.Component;
79

810
@Component
911
public class JerseyConfig extends ResourceConfig {
1012

1113
public JerseyConfig() {
14+
register( ToJsonExceptionMapper.class );
15+
1216
registerEndpoints();
1317
}
1418

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Hibernate Search, full-text search for your domain model
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.search.demos.wikipedia.endpoint.error;
8+
9+
import javax.ws.rs.core.Response;
10+
11+
public class ErrorEntity {
12+
private Response.Status status;
13+
private String message;
14+
private String stackTrace;
15+
16+
public Response.Status getStatus() {
17+
return status;
18+
}
19+
20+
public void setStatus(Response.Status status) {
21+
this.status = status;
22+
}
23+
24+
public String getMessage() {
25+
return message;
26+
}
27+
28+
public void setMessage(String message) {
29+
this.message = message;
30+
}
31+
32+
public String getStackTrace() {
33+
return stackTrace;
34+
}
35+
36+
public void setStackTrace(String stackTrace) {
37+
this.stackTrace = stackTrace;
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Hibernate Search, full-text search for your domain model
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.search.demos.wikipedia.endpoint.error;
8+
9+
import java.io.PrintWriter;
10+
import java.io.StringWriter;
11+
import javax.ws.rs.WebApplicationException;
12+
import javax.ws.rs.core.MediaType;
13+
import javax.ws.rs.core.Response;
14+
import javax.ws.rs.ext.ExceptionMapper;
15+
16+
public class ToJsonExceptionMapper implements ExceptionMapper<Throwable> {
17+
@Override
18+
public Response toResponse(Throwable ex) {
19+
ErrorEntity errorEntity = new ErrorEntity();
20+
Response.Status status;
21+
if ( ex instanceof WebApplicationException ) {
22+
Response originalResponse = ( (WebApplicationException) ex ).getResponse();
23+
status = Response.Status.fromStatusCode( originalResponse.getStatus() );
24+
}
25+
else {
26+
status = Response.Status.INTERNAL_SERVER_ERROR;
27+
}
28+
29+
errorEntity.setStatus( status );
30+
errorEntity.setMessage( ex.getMessage() );
31+
32+
StringWriter errorStackTrace = new StringWriter();
33+
ex.printStackTrace( new PrintWriter( errorStackTrace ) );
34+
errorEntity.setStackTrace( errorStackTrace.toString() );
35+
36+
return Response.status( status )
37+
.entity( errorEntity )
38+
.type( MediaType.APPLICATION_JSON )
39+
.build();
40+
}
41+
}

0 commit comments

Comments
 (0)