102 lines
4.4 KiB
Java
102 lines
4.4 KiB
Java
package com.iconplus.smartproc.exception;
|
|
|
|
import lombok.extern.log4j.Log4j2;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.validation.FieldError;
|
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
import org.springframework.web.client.HttpServerErrorException;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Objects;
|
|
import java.util.concurrent.TimeoutException;
|
|
|
|
@Log4j2
|
|
@ControllerAdvice
|
|
public class GlobalExceptionHandler {
|
|
|
|
private ErrorHelper errorHelper;
|
|
|
|
public GlobalExceptionHandler(ErrorHelper errorHelper) {
|
|
this.errorHelper = errorHelper;
|
|
}
|
|
|
|
private Boolean isError = false;
|
|
private static final String DEFAULT_ERROR_CODE = "80000";
|
|
|
|
@ExceptionHandler(Exception.class)
|
|
public ResponseEntity<ErrorResponse> renderDefaultResponse(Exception ex) {
|
|
log.error("Exception occurred: ", ex);
|
|
|
|
return errorHelper.throwErrorException(DEFAULT_ERROR_CODE, HttpStatus.INTERNAL_SERVER_ERROR);
|
|
}
|
|
|
|
@ExceptionHandler(BusinessException.class)
|
|
public ResponseEntity<ErrorResponse> renderBusinessErrorResponse(BusinessException exception) {
|
|
log.error("BusinessException occurred: ", exception);
|
|
if (Objects.nonNull(exception.getErrorMessageMap())) {
|
|
return getErrorResponseResponseEntity(exception.getErrorDesc(), exception.getErrorMessage(), exception.getErrorCode(), exception.getHttpStatus(), exception.getErrorMessageMap());
|
|
}
|
|
|
|
return getErrorResponseResponseEntity(exception.getErrorDesc(), exception.getErrorMessage(), exception.getErrorCode(), exception.getHttpStatus(), null);
|
|
}
|
|
|
|
|
|
@ExceptionHandler(TechnicalException.class)
|
|
public ResponseEntity<ErrorResponse> renderTechnicalErrorResponse(TechnicalException exception) {
|
|
log.error("TechnicalException occurred: ", exception);
|
|
|
|
return getErrorResponseResponseEntity(exception.getErrorDesc(), exception.getErrorMessage(), exception.getErrorCode(), exception.getHttpStatus(), null);
|
|
}
|
|
|
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
|
public ResponseEntity<ErrorResponse> renderMethodArgumentErrorResponse(MethodArgumentNotValidException exception) {
|
|
log.error("MethodArgumentNotValidException occurred: ", exception);
|
|
|
|
List<String> errors = new ArrayList<String>();
|
|
|
|
for (FieldError error : exception.getBindingResult().getFieldErrors()) {
|
|
errors.add(error.getField().concat(":").concat(error.getDefaultMessage()));
|
|
}
|
|
|
|
return errorHelper.throwErrorExceptionWithMessage(HttpStatus.BAD_REQUEST, "80400", "Request Validation Error", errors.toString());
|
|
}
|
|
|
|
@ExceptionHandler(HttpServerErrorException.class)
|
|
public ResponseEntity<ErrorResponse> renderHttpServerErrorResponse(HttpServerErrorException exception) {
|
|
log.error("HttpServerErrorException occurred: ", exception);
|
|
|
|
return errorHelper.throwErrorException(DEFAULT_ERROR_CODE, HttpStatus.INTERNAL_SERVER_ERROR);
|
|
}
|
|
|
|
@ExceptionHandler(TimeoutException.class)
|
|
public ResponseEntity<ErrorResponse> renderTimeoutResponse(TimeoutException exception) {
|
|
log.error("TimeoutException occurred: ", exception);
|
|
|
|
return errorHelper.throwErrorException("80001", HttpStatus.INTERNAL_SERVER_ERROR);
|
|
}
|
|
|
|
private ResponseEntity<ErrorResponse> getErrorResponseResponseEntity(String errorDesc, String errorMessage,
|
|
String errorCode, HttpStatus httpStatus,
|
|
Map<String, String> metadata) {
|
|
if (StringUtils.isNotBlank(errorDesc) && StringUtils.isNotBlank(errorMessage)) {
|
|
|
|
return errorHelper.throwErrorExceptionWithMessageAndMetadataWithoutLocalization(httpStatus, errorCode, errorDesc, errorMessage, metadata);
|
|
} else {
|
|
if (Objects.nonNull(metadata)) {
|
|
return errorHelper.throwErrorExceptionWithMetadata(errorCode, httpStatus, metadata);
|
|
}
|
|
|
|
return errorHelper.throwErrorException(errorCode, httpStatus);
|
|
}
|
|
}
|
|
|
|
public Boolean isError() {
|
|
return isError;
|
|
}
|
|
} |