1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.maxur.perfmodel.backend.rest;
17
18 import org.maxur.perfmodel.backend.domain.Incident;
19 import org.slf4j.Logger;
20
21 import javax.ws.rs.core.GenericEntity;
22 import javax.ws.rs.core.Response;
23 import javax.ws.rs.ext.ExceptionMapper;
24 import java.util.List;
25
26 import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
27 import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
28 import static org.maxur.perfmodel.backend.domain.Incident.incidents;
29 import static org.slf4j.LoggerFactory.getLogger;
30
31
32
33
34
35
36 public class RuntimeExceptionHandler implements ExceptionMapper<RuntimeException> {
37
38 private static final Logger LOGGER = getLogger(RuntimeExceptionHandler.class);
39
40 @Override
41 public Response toResponse(RuntimeException exception) {
42 LOGGER.error(exception.getMessage(), exception);
43 return Response
44 .status(INTERNAL_SERVER_ERROR)
45 .type(APPLICATION_JSON)
46 .entity(makeErrorEntity(exception))
47 .build();
48 }
49
50 private GenericEntity<List<Incident>> makeErrorEntity(final RuntimeException exception) {
51 return new GenericEntity<List<Incident>>(incidents("System error", exception.getMessage())) {
52 };
53 }
54
55
56 }