View Javadoc

1   /*
2    * Copyright (c) 2015 Maxim Yunusov
3    *    Licensed under the Apache License, Version 2.0 (the "License");
4    *    you may not use this file except in compliance with the License.
5    *    You may obtain a copy of the License at
6    *
7    *        http://www.apache.org/licenses/LICENSE-2.0
8    *
9    *    Unless required by applicable law or agreed to in writing, software
10   *    distributed under the License is distributed on an "AS IS" BASIS,
11   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   *    See the License for the specific language governing permissions and
13   *    limitations under the License.
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   * @author myunusov
33   * @version 1.0
34   * @since <pre>11.09.2015</pre>
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  }