Skip to content

Commit 085e770

Browse files
ahornaceVladimir Kotal
authored andcommitted
Return JSON representation of exception on REST API error
1 parent 0600896 commit 085e770

File tree

4 files changed

+105
-10
lines changed

4 files changed

+105
-10
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
22+
*/
23+
package org.opensolaris.opengrok.web.api.error;
24+
25+
import javax.ws.rs.core.MediaType;
26+
import javax.ws.rs.core.Response;
27+
28+
public class ExceptionMapperUtils {
29+
30+
private ExceptionMapperUtils() {
31+
}
32+
33+
public static Response toResponse(final Response.Status status, final Exception e) {
34+
return Response.status(status)
35+
.entity(new ExceptionModel(e.getMessage()))
36+
.type(MediaType.APPLICATION_JSON)
37+
.build();
38+
}
39+
40+
private static class ExceptionModel {
41+
42+
private String message;
43+
44+
public ExceptionModel(final String message) {
45+
this.message = message;
46+
}
47+
48+
public String getMessage() {
49+
return message;
50+
}
51+
52+
public void setMessage(final String message) {
53+
this.message = message;
54+
}
55+
}
56+
57+
}

src/org/opensolaris/opengrok/web/api/error/GenericExceptionMapper.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,24 @@
2222
*/
2323
package org.opensolaris.opengrok.web.api.error;
2424

25-
import javax.ws.rs.core.MediaType;
25+
import org.opensolaris.opengrok.logger.LoggerFactory;
26+
2627
import javax.ws.rs.core.Response;
2728
import javax.ws.rs.ext.ExceptionMapper;
2829
import javax.ws.rs.ext.Provider;
30+
import java.util.logging.Level;
31+
import java.util.logging.Logger;
2932

3033
@Provider
3134
public class GenericExceptionMapper implements ExceptionMapper<Exception> {
3235

36+
private static final Logger logger = LoggerFactory.getLogger(GenericExceptionMapper.class);
37+
3338
@Override
3439
public Response toResponse(final Exception e) {
35-
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
36-
.entity(e.getMessage())
37-
.type(MediaType.TEXT_PLAIN)
38-
.build();
40+
logger.log(Level.FINE, "Exception while processing request", e);
41+
42+
return ExceptionMapperUtils.toResponse(Response.Status.INTERNAL_SERVER_ERROR, e);
3943
}
4044

4145
}

src/org/opensolaris/opengrok/web/api/error/ValidationExceptionMapper.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
package org.opensolaris.opengrok.web.api.error;
2424

2525
import javax.validation.ValidationException;
26-
import javax.ws.rs.core.MediaType;
2726
import javax.ws.rs.core.Response;
2827
import javax.ws.rs.ext.ExceptionMapper;
2928
import javax.ws.rs.ext.Provider;
@@ -33,10 +32,7 @@ public class ValidationExceptionMapper implements ExceptionMapper<ValidationExce
3332

3433
@Override
3534
public Response toResponse(final ValidationException e) {
36-
return Response.status(Response.Status.BAD_REQUEST)
37-
.entity(e.getMessage())
38-
.type(MediaType.TEXT_PLAIN)
39-
.build();
35+
return ExceptionMapperUtils.toResponse(Response.Status.BAD_REQUEST, e);
4036
}
4137

4238
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
22+
*/
23+
package org.opensolaris.opengrok.web.api.error;
24+
25+
import javax.ws.rs.WebApplicationException;
26+
import javax.ws.rs.core.Response;
27+
import javax.ws.rs.ext.ExceptionMapper;
28+
import javax.ws.rs.ext.Provider;
29+
30+
@Provider
31+
public class WebApplicationExceptionMapper implements ExceptionMapper<WebApplicationException> {
32+
33+
@Override
34+
public Response toResponse(final WebApplicationException e) {
35+
return e.getResponse();
36+
}
37+
38+
}

0 commit comments

Comments
 (0)