新版仿ok交易所-后端
1
zj
8 days ago 579177ac64462d0fec885eb10af3097245134f80
trading-order-common/src/main/java/com/yami/trading/common/config/DefaultExceptionHandlerConfig.java
@@ -13,6 +13,7 @@
import com.yami.trading.common.domain.Result;
import com.yami.trading.common.exception.BusinessException;
import com.yami.trading.common.exception.YamiShopBindException;
import com.yami.trading.common.util.ApiMessageTranslator;
import com.yami.trading.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
@@ -44,7 +45,7 @@
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result <String> exception(MethodArgumentNotValidException e) {
    public Result <String> exception(MethodArgumentNotValidException e, HttpServletRequest req) {
        BindingResult bindingResult = e.getBindingResult();
        List<ObjectError> allErrors = bindingResult.getAllErrors();
        List<ErrorMsg> errorMsgs = new ArrayList<>();
@@ -56,7 +57,7 @@
            errorMsg.setMessage(fieldError.getDefaultMessage());
            errorMsgs.add(errorMsg);
        });
        return Result.failed(errorMsgs.toString());    }
        return Result.failed(translateForUserClient(req, errorMsgs.toString()));    }
    @ExceptionHandler(value = ConstraintViolationException.class)
@@ -89,7 +90,7 @@
        log.error("{}", ex);
        String message = ex.getMessage();
        if(StringUtils.isNotEmpty(message)){
            return Result.failed(message);
            return Result.failed(translateForUserClient(req, message));
        }else{
            return Result.failed(ex.toString());
        }
@@ -98,13 +99,23 @@
    @ExceptionHandler(YamiShopBindException.class)
    public Result unauthorizedExceptionHandler(YamiShopBindException e){
    public Result unauthorizedExceptionHandler(YamiShopBindException e, HttpServletRequest req){
        log.error("YamiException Message :{}",e.getMessage());
        return  Result.of(null,e.getCode(),e.getMessage());
        return  Result.of(null,e.getCode(),translateForUserClient(req, e.getMessage()));
    }
    @ExceptionHandler(BusinessException.class)
    public Result businessExceptionHandler(BusinessException e){
    public Result businessExceptionHandler(BusinessException e, HttpServletRequest req){
        log.error("YamiException Message :{}",e.getMessage());
        return  Result.failed(e.getMessage());
        return  Result.failed(translateForUserClient(req, e.getMessage()));
    }
    private static String translateForUserClient(HttpServletRequest req, String message) {
        if (req == null || message == null) {
            return message;
        }
        if (ApiMessageTranslator.isUserApiRequest(req.getRequestURI())) {
            return ApiMessageTranslator.toEnglish(message);
        }
        return message;
    }
}