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