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 info.magnolia.jcr.RuntimeRepositoryException; |
37 |
|
|
38 |
|
import javax.jcr.AccessDeniedException; |
39 |
|
import javax.jcr.NamespaceException; |
40 |
|
import javax.jcr.ValueFormatException; |
41 |
|
import javax.jcr.nodetype.NoSuchNodeTypeException; |
42 |
|
import javax.jcr.query.InvalidQueryException; |
43 |
|
import javax.ws.rs.core.MediaType; |
44 |
|
import javax.ws.rs.core.Response; |
45 |
|
import javax.ws.rs.ext.ExceptionMapper; |
46 |
|
import javax.ws.rs.ext.Provider; |
47 |
|
|
48 |
|
import org.slf4j.Logger; |
49 |
|
import org.slf4j.LoggerFactory; |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
@Provider |
|
|
| 62.5% |
Uncovered Elements: 6 (16) |
Complexity: 8 |
Complexity Density: 0.89 |
|
55 |
|
public class RestExceptionMapper implements ExceptionMapper<Throwable> { |
56 |
|
|
57 |
|
private final static Logger log = LoggerFactory.getLogger(RestExceptionMapper.class); |
58 |
|
|
|
|
| 60% |
Uncovered Elements: 6 (15) |
Complexity: 8 |
Complexity Density: 0.89 |
|
59 |
3 |
@Override... |
60 |
|
public Response toResponse(Throwable exception) { |
61 |
|
|
62 |
3 |
Response.Status status = Response.Status.INTERNAL_SERVER_ERROR; |
63 |
|
|
64 |
3 |
if (exception instanceof AccessDeniedException) { |
65 |
1 |
status = Response.Status.FORBIDDEN; |
66 |
2 |
} else if (exception instanceof RuntimeRepositoryException && exception.getCause() != null) { |
67 |
0 |
Throwable cause = exception.getCause(); |
68 |
0 |
if (cause instanceof NoSuchNodeTypeException |
69 |
|
|| cause instanceof NamespaceException |
70 |
|
|| cause instanceof InvalidQueryException |
71 |
|
|| cause instanceof ValueFormatException) { |
72 |
0 |
status = Response.Status.BAD_REQUEST; |
73 |
|
} |
74 |
|
} |
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
3 |
log.error("Exception thrown executing REST endpoint, returning {}", status.getStatusCode(), exception); |
81 |
|
|
82 |
3 |
return Response.status(status).entity(exception.getMessage()).type(MediaType.TEXT_PLAIN_TYPE).build(); |
83 |
|
} |
84 |
|
} |