| | |
| | | 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; |
| | |
| | | |
| | | |
| | | @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<>(); |
| | |
| | | errorMsg.setMessage(fieldError.getDefaultMessage()); |
| | | errorMsgs.add(errorMsg); |
| | | }); |
| | | return Result.failed(errorMsgs.toString()); } |
| | | return Result.failed(translateForUserClient(req, errorMsgs.toString())); } |
| | | |
| | | |
| | | @ExceptionHandler(value = ConstraintViolationException.class) |
| | |
| | | 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()); |
| | | } |
| | |
| | | |
| | | |
| | | @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; |
| | | } |
| | | } |