1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
package info.magnolia.rest; |
35 |
|
|
36 |
|
import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; |
37 |
|
|
38 |
|
import javax.ws.rs.BadRequestException; |
39 |
|
import javax.ws.rs.NotAcceptableException; |
40 |
|
import javax.ws.rs.NotAllowedException; |
41 |
|
import javax.ws.rs.NotAuthorizedException; |
42 |
|
import javax.ws.rs.NotFoundException; |
43 |
|
import javax.ws.rs.WebApplicationException; |
44 |
|
import javax.ws.rs.container.ResourceInfo; |
45 |
|
import javax.ws.rs.core.Context; |
46 |
|
import javax.ws.rs.core.MediaType; |
47 |
|
import javax.ws.rs.core.Response; |
48 |
|
import javax.ws.rs.core.Response.StatusType; |
49 |
|
import javax.ws.rs.ext.ExceptionMapper; |
50 |
|
import javax.ws.rs.ext.Provider; |
51 |
|
|
52 |
|
import org.apache.commons.lang3.StringUtils; |
53 |
|
import org.jboss.resteasy.spi.ReaderException; |
54 |
|
import org.jboss.resteasy.spi.WriterException; |
55 |
|
import org.slf4j.Logger; |
56 |
|
import org.slf4j.LoggerFactory; |
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
@Provider |
|
|
| 66% |
Uncovered Elements: 16 (47) |
Complexity: 12 |
Complexity Density: 0.46 |
|
62 |
|
public class RestExceptionMapper implements ExceptionMapper<Throwable> { |
63 |
|
private static final Logger log = LoggerFactory.getLogger(RestExceptionMapper.class); |
64 |
|
|
65 |
|
@Context |
66 |
|
private ResourceInfo resourceInfo; |
67 |
|
|
|
|
| 65.2% |
Uncovered Elements: 16 (46) |
Complexity: 12 |
Complexity Density: 0.46 |
|
68 |
6 |
@Override... |
69 |
|
public Response toResponse(Throwable exception) { |
70 |
6 |
StatusType status = INTERNAL_SERVER_ERROR; |
71 |
6 |
String errorCode = "unknown"; |
72 |
6 |
String errorMessage = exception.getMessage(); |
73 |
|
|
74 |
6 |
if (exception instanceof WebApplicationException) { |
75 |
4 |
if (exception instanceof NotAuthorizedException) { |
76 |
0 |
errorCode = "notAuthorized"; |
77 |
4 |
} else if (exception instanceof BadRequestException) { |
78 |
0 |
errorCode = "badRequest"; |
79 |
4 |
} else if (exception instanceof NotAllowedException) { |
80 |
1 |
errorCode = "methodNotAllowed"; |
81 |
3 |
} else if (exception instanceof NotAcceptableException) { |
82 |
0 |
errorCode = "notAcceptable"; |
83 |
3 |
} else if (exception instanceof NotFoundException) { |
84 |
3 |
errorCode = "notFound"; |
85 |
0 |
} else if (exception instanceof ReaderException) { |
86 |
0 |
errorCode = "readerError"; |
87 |
0 |
} else if (exception instanceof WriterException) { |
88 |
0 |
errorCode = "writerError"; |
89 |
|
} |
90 |
|
|
91 |
|
|
92 |
4 |
if (errorMessage.startsWith("RESTEASY")) { |
93 |
1 |
errorMessage = StringUtils.substringAfter(errorMessage, ": "); |
94 |
|
} |
95 |
|
|
96 |
4 |
Response response = ((WebApplicationException) exception).getResponse(); |
97 |
4 |
status = response.getStatusInfo(); |
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
4 |
if (exception instanceof NotAcceptableException |
107 |
|
|| resourceInfo.getResourceMethod() == null) { |
108 |
4 |
return Response.status(status) |
109 |
|
.type(MediaType.APPLICATION_JSON) |
110 |
|
.entity(new RestError(errorCode, errorMessage)) |
111 |
|
.build(); |
112 |
|
} |
113 |
|
} |
114 |
|
|
115 |
2 |
log.error("Exception thrown executing REST endpoint, returning {}", status.getStatusCode(), exception); |
116 |
|
|
117 |
2 |
return Response.status(status) |
118 |
|
.entity(new RestError(errorCode, errorMessage)) |
119 |
|
.build(); |
120 |
|
} |
121 |
|
} |