001/* 002 * Copyright (c) 2015 Maxim Yunusov 003 * Licensed under the Apache License, Version 2.0 (the "License"); 004 * you may not use this file except in compliance with the License. 005 * You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software 010 * distributed under the License is distributed on an "AS IS" BASIS, 011 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 * See the License for the specific language governing permissions and 013 * limitations under the License. 014 */ 015 016package org.maxur.perfmodel.backend.rest; 017 018import org.maxur.perfmodel.backend.domain.Incident; 019import org.slf4j.Logger; 020 021import javax.ws.rs.core.GenericEntity; 022import javax.ws.rs.core.Response; 023import javax.ws.rs.ext.ExceptionMapper; 024import java.util.List; 025 026import static javax.ws.rs.core.MediaType.APPLICATION_JSON; 027import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; 028import static org.maxur.perfmodel.backend.domain.Incident.incidents; 029import static org.slf4j.LoggerFactory.getLogger; 030 031/** 032 * @author myunusov 033 * @version 1.0 034 * @since <pre>11.09.2015</pre> 035 */ 036public class RuntimeExceptionHandler implements ExceptionMapper<RuntimeException> { 037 038 private static final Logger LOGGER = getLogger(RuntimeExceptionHandler.class); 039 040 @Override 041 public Response toResponse(RuntimeException exception) { 042 LOGGER.error(exception.getMessage(), exception); 043 return Response 044 .status(INTERNAL_SERVER_ERROR) 045 .type(APPLICATION_JSON) 046 .entity(makeErrorEntity(exception)) 047 .build(); 048 } 049 050 private GenericEntity<List<Incident>> makeErrorEntity(final RuntimeException exception) { 051 return new GenericEntity<List<Incident>>(incidents("System error", exception.getMessage())) { 052 }; 053 } 054 055 056}