improved logging
This commit is contained in:
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user