improved logging

This commit is contained in:
2018-10-22 19:07:03 +02:00
parent 95d93ec222
commit d3e0e09718

View File

@ -1,61 +1,70 @@
/* /*
* Copyright 2018 joern (at) muehlencord.de * Copyright 2018 joern (at) muehlencord.de
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package de.muehlencord.shared.jeeutil.restexfw; package de.muehlencord.shared.jeeutil.restexfw;
import java.util.Locale; import java.util.Locale;
import javax.inject.Inject; import javax.inject.Inject;
import javax.interceptor.AroundInvoke; import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext; import javax.interceptor.InvocationContext;
import javax.validation.ConstraintViolationException; import javax.validation.ConstraintViolationException;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import org.slf4j.Logger;
/** import org.slf4j.LoggerFactory;
*
* @author jomu /**
*/ *
public class APIExceptionInterceptor { * @author jomu
*/
@Inject public class APIExceptionInterceptor {
Locale locale;
private static final Logger LOGGER = LoggerFactory.getLogger(APIExceptionInterceptor.class);
@AroundInvoke
public Object handleException(InvocationContext context) { @Inject
Object proceedResponse; Locale locale;
try {
// continue to execute context @AroundInvoke
// if an exception is thrown during processing, this is passed in to the catch block below public Object handleException(InvocationContext context) {
proceedResponse = context.proceed(); Object proceedResponse;
} catch (Exception ex) { try {
Response errorResponse; // continue to execute context
if (ex instanceof APIException) { // if an exception is thrown during processing, this is passed in to the catch block below
errorResponse = ((APIException) ex).getHttpResponse(); proceedResponse = context.proceed();
} else if (ex.getCause() instanceof APIException) { } catch (Exception ex) {
errorResponse = ((APIException) ex.getCause()).getHttpResponse(); Response errorResponse;
} else if (ex instanceof ConstraintViolationException) { if (ex instanceof APIException) {
// this exception is handled via the ConstraintViolationMapper errorResponse = ((APIException) ex).getHttpResponse();
throw (ConstraintViolationException) ex; } else if (ex.getCause() instanceof APIException) {
} else if (ex.getCause() instanceof ConstraintViolationException) { errorResponse = ((APIException) ex.getCause()).getHttpResponse();
// this exception is handled via the ConstraintViolationMapper } else if (ex instanceof ConstraintViolationException) {
throw (ConstraintViolationException) ex.getCause(); // this exception is handled via the ConstraintViolationMapper
} else { throw (ConstraintViolationException) ex;
errorResponse = new APIException(ex, locale).getHttpResponse(); } else if (ex.getCause() instanceof ConstraintViolationException) {
} // this exception is handled via the ConstraintViolationMapper
return errorResponse; throw (ConstraintViolationException) ex.getCause();
} } else {
return proceedResponse; errorResponse = new APIException(ex, locale).getHttpResponse();
} }
} if (LOGGER.isDebugEnabled()) {
LOGGER.debug(ex.toString(), ex);
}
return errorResponse;
}
return proceedResponse;
}
}