import com.lh.digital.integration.model.HttpStatusResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import javax.annotation.PostConstruct;
import javax.validation.ValidationException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ControllerAdvice
public class ExceptionInterceptor {
private final Logger logger = LoggerFactory.getLogger(ExceptionInterceptor.class);
/* @Value("${httpStatusResponse}")
private Map execeptionMap = new HashMap();*/
//JSR-303 Validation Exceptions
@ExceptionHandler(ValidationException.class)
public ResponseEntity<HttpStatusResponse> exceptionHandler(ValidationException ex) {
logger.info("++++++++++++++++++++ exceptionHandler ++++++++++++++++++++");
//logger.info("++++++++++++++++++++ Size: " + execeptionMap.size() + " ++++++++++++++++++++");
logger.info(" exceptionHandler ValidationException Message: " + ex.getMessage());
StackTraceElement[] stackTrace = ex.getStackTrace();
for (StackTraceElement item : stackTrace)
logger.info(item.toString());
return createErrorResponse(400, ex, false, HttpStatus.BAD_REQUEST);
}
//List of Exceptions we can retry
@ExceptionHandler(NullPointerException.class)
public ResponseEntity<HttpStatusResponse> exceptionHandler(NullPointerException ex) {
logger.info("++++++++++++++++++++ exceptionHandler ++++++++++++++++++++");
//logger.info("++++++++++++++++++++ Size: " + execeptionMap.size() + " ++++++++++++++++++++");
logger.info(" exceptionHandler NullPointerException Message: " + ex.getMessage());
return createErrorResponse(409, ex, true, HttpStatus.CONFLICT);
}
//Unhandled Fatal Exceptions
@ExceptionHandler(Exception.class)
public ResponseEntity<HttpStatusResponse> exceptionHandler(Exception ex) {
logger.info("++++++++++++++++++++ exceptionHandler ++++++++++++++++++++");
//logger.info("++++++++++++++++++++ Size: " + execeptionMap.size() + " ++++++++++++++++++++");
logger.info(" exceptionHandler Exception Message: " + ex.getMessage());
logger.info(ex.getMessage());
return createErrorResponse(500, ex, false, HttpStatus.EXPECTATION_FAILED);
}
// Helpers
private ResponseEntity<HttpStatusResponse> createErrorResponse(int responseCode, Exception ex, boolean isRetryable, HttpStatus status){
HttpStatusResponse response = new HttpStatusResponse();
response.setMessage(ex.getMessage());
response.setResponseCode(responseCode);
response.setRetryable(isRetryable);
return new ResponseEntity<>(response, status);
}
}
This class can be used as global exceptino handler like common utility. This reduces maximum coding w.r.t exception handling.
The @ControllerAdvice will take care if you add this in your project. The intercepter will be invoked if there is any error exption occured at execution time,
The @ControllerAdvice will take care if you add this in your project. The intercepter will be invoked if there is any error exption occured at execution time,
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.