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.slf4j.Logger; |
54 |
|
import org.slf4j.LoggerFactory; |
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
@Provider |
|
|
| 62% |
Uncovered Elements: 19 (50) |
Complexity: 13 |
Complexity Density: 0.45 |
|
60 |
|
public class RestExceptionMapper implements ExceptionMapper<Throwable> { |
61 |
|
private static final Logger log = LoggerFactory.getLogger(RestExceptionMapper.class); |
62 |
|
|
63 |
|
@Context |
64 |
|
private ResourceInfo resourceInfo; |
65 |
|
|
|
|
| 61.2% |
Uncovered Elements: 19 (49) |
Complexity: 13 |
Complexity Density: 0.45 |
|
66 |
6 |
@Override... |
67 |
|
public Response toResponse(Throwable exception) { |
68 |
6 |
StatusType status = INTERNAL_SERVER_ERROR; |
69 |
6 |
String errorCode = "unknown"; |
70 |
6 |
String errorMessage = exception.getMessage(); |
71 |
|
|
72 |
6 |
if (exception instanceof WebApplicationException) { |
73 |
4 |
if (exception instanceof NotAuthorizedException) { |
74 |
0 |
errorCode = "notAuthorized"; |
75 |
4 |
} else if (exception instanceof BadRequestException) { |
76 |
0 |
errorCode = "badRequest"; |
77 |
4 |
} else if (exception instanceof NotAllowedException) { |
78 |
1 |
errorCode = "methodNotAllowed"; |
79 |
3 |
} else if (exception instanceof NotAcceptableException) { |
80 |
0 |
errorCode = "notAcceptable"; |
81 |
3 |
} else if (exception instanceof NotFoundException) { |
82 |
3 |
errorCode = "notFound"; |
83 |
|
} else { |
84 |
0 |
try { |
85 |
0 |
Class<?> restEasyReaderEx = Class.forName("org.jboss.resteasy.spi.ReaderException"); |
86 |
0 |
Class<?> restEasyWriterEx = Class.forName("org.jboss.resteasy.spi.WriterException"); |
87 |
0 |
if (restEasyReaderEx.isAssignableFrom(exception.getClass())) { |
88 |
0 |
errorCode = "readerError"; |
89 |
0 |
} else if (restEasyWriterEx.isAssignableFrom(exception.getClass())) { |
90 |
0 |
errorCode = "writerError"; |
91 |
|
} |
92 |
|
} catch (ClassNotFoundException e) { |
93 |
|
|
94 |
|
} |
95 |
|
} |
96 |
|
|
97 |
|
|
98 |
4 |
if (errorMessage.startsWith("RESTEASY")) { |
99 |
1 |
errorMessage = StringUtils.substringAfter(errorMessage, ": "); |
100 |
|
} |
101 |
|
|
102 |
4 |
Response response = ((WebApplicationException) exception).getResponse(); |
103 |
4 |
status = response.getStatusInfo(); |
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
4 |
if (exception instanceof NotAcceptableException |
113 |
|
|| resourceInfo.getResourceMethod() == null) { |
114 |
4 |
return Response.status(status) |
115 |
|
.type(MediaType.APPLICATION_JSON) |
116 |
|
.entity(new RestError(errorCode, errorMessage)) |
117 |
|
.build(); |
118 |
|
} |
119 |
|
} |
120 |
|
|
121 |
2 |
log.error("Exception thrown executing REST endpoint, returning {}", status.getStatusCode(), exception); |
122 |
|
|
123 |
2 |
return Response.status(status) |
124 |
|
.entity(new RestError(errorCode, errorMessage)) |
125 |
|
.build(); |
126 |
|
} |
127 |
|
} |