72 lines
3.1 KiB
Java
72 lines
3.1 KiB
Java
package com.darkness.common.base;
|
|
|
|
import com.darkness.common.PublicConst;
|
|
import com.darkness.common.util.response.RestResponse;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.http.HttpHeaders;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.validation.BindException;
|
|
import org.springframework.validation.FieldError;
|
|
import org.springframework.web.bind.WebDataBinder;
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
import org.springframework.web.bind.annotation.InitBinder;
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
|
@Slf4j
|
|
@RestControllerAdvice
|
|
public class ControllerBase {
|
|
|
|
@ExceptionHandler(Exception.class)
|
|
public ResponseEntity<Object> handleException(Exception ex) {
|
|
RestResponse restResponse;
|
|
if (ex instanceof RuntimeErrorException) {
|
|
RuntimeErrorException errorException = (RuntimeErrorException) ex;
|
|
|
|
String errorDetail = getErrorDetail(ex);
|
|
log.error(String.format("Exception in processStat request [%s]: %s",WebRequestSequence.current().getRequestSequence(),errorDetail));
|
|
restResponse = new RestResponse(
|
|
errorException.getErrorScope(), errorException.getErrorCode(), errorDetail);
|
|
restResponse.setErrorDetails(errorDetail);
|
|
|
|
} else if(ex instanceof BindException){
|
|
log.error(String.format("Exception in processStat request [%s]: %s", WebRequestSequence.current().getRequestSequence(), ex.getMessage() != null ? ex.getMessage() : ""), ex);
|
|
BindException bindException = (BindException)ex;
|
|
FieldError fieldError = bindException.getFieldError();
|
|
String exMessage = null;
|
|
if (fieldError != null) {
|
|
exMessage = fieldError.getDefaultMessage();
|
|
}
|
|
restResponse = new RestResponse(
|
|
PublicConst.SCOPE_GENERAL, PublicConst.ERROR_GENERAL_EXCEPTION, exMessage);
|
|
restResponse.setErrorDetails(exMessage);
|
|
}else {
|
|
log.error(String.format("Exception in processStat request [%s]: %s", WebRequestSequence.current().getRequestSequence(), ex.getMessage() != null ? ex.getMessage() : ""), ex);
|
|
String exMessage = ex.toString();
|
|
restResponse = new RestResponse(
|
|
PublicConst.SCOPE_GENERAL, PublicConst.ERROR_GENERAL_EXCEPTION, exMessage);
|
|
restResponse.setErrorDetails(exMessage);
|
|
}
|
|
|
|
HttpHeaders responseHeaders = new HttpHeaders();
|
|
responseHeaders.set("Content-Type", "application/json; charset=utf-8");
|
|
|
|
return new ResponseEntity<>(restResponse, responseHeaders, HttpStatus.OK);
|
|
}
|
|
|
|
private String getErrorDetail(Exception ex) {
|
|
String errorDetail;
|
|
if (ex.getCause() != null) {
|
|
errorDetail = ex.getCause().toString();
|
|
} else {
|
|
errorDetail = ex.toString();
|
|
}
|
|
return errorDetail;
|
|
}
|
|
|
|
@InitBinder
|
|
public void initListBinder(WebDataBinder binder) {
|
|
binder.setAutoGrowCollectionLimit(Integer.MAX_VALUE);
|
|
}
|
|
}
|