Commit 81952aa1a72eb18ee2ef2562774834d2c4fe865e

Authored by xp.Huang
1 parent e456de06

fix:调整异常捕获

... ... @@ -20,6 +20,7 @@ import org.thingsboard.server.common.data.yunteng.core.exception.*;
20 20 import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage;
21 21 import org.thingsboard.server.common.data.yunteng.dto.SysLogOperateDTO;
22 22 import org.thingsboard.server.dao.audit.AuditLogService;
  23 +import org.thingsboard.server.dao.exception.DataValidationException;
23 24 import org.thingsboard.server.exception.ThingsboardErrorResponseHandler;
24 25 import org.thingsboard.server.service.security.model.SecurityUser;
25 26 import org.thingsboard.server.utils.yunteng.LogUtils;
... ... @@ -36,22 +37,27 @@ public class ThingsKitExceptionHandler {
36 37 private final AuditLogService auditLogService;
37 38 private final ThingsboardErrorResponseHandler errorResponseHandler;
38 39
39   - @ExceptionHandler(RuntimeException.class)
  40 + @ExceptionHandler(MethodArgumentNotValidException.class)
40 41 public void handleMethodArgumentNotValidException(
41   - RuntimeException ex,
42   - HttpServletRequest request,
43   - HttpServletResponse response) {
44   - if(ex instanceof ThingsKitException){
45   - errorResponseHandler.handle(
46   - new ThingsKitException(
47   - ErrorMessage.INVALID_PARAMETER.setMessage(
48   - ex.getMessage()),
49   - HttpStatus.BAD_REQUEST),
50   - response);
51   - }else{
  42 + MethodArgumentNotValidException ex,
  43 + HttpServletRequest request,
  44 + HttpServletResponse response) {
  45 + produceLog(request, ex);
  46 + errorResponseHandler.handle(
  47 + new ThingsKitException(
  48 + ErrorMessage.INVALID_PARAMETER.setMessage(
  49 + Objects.requireNonNull(ex.getBindingResult().getFieldError()).getDefaultMessage()),
  50 + HttpStatus.BAD_REQUEST),
  51 + response);
  52 + }
  53 +
  54 + @ExceptionHandler(RuntimeException.class)
  55 + public void handleRuntimeException(
  56 + RuntimeException ex, HttpServletRequest request, HttpServletResponse response) {
  57 + if(!(ex instanceof ThingsKitException) && !(ex instanceof DataValidationException)){
52 58 produceLog(request, ex);
53   - errorResponseHandler.handle(ex,response);
54 59 }
  60 + errorResponseHandler.handle(ex,response);
55 61 }
56 62
57 63 protected SecurityUser getCurrentUser() throws ThingsboardException {
... ...
... ... @@ -27,6 +27,7 @@ import org.thingsboard.server.common.data.id.RuleChainId;
27 27 import org.thingsboard.server.common.data.id.TenantId;
28 28 import org.thingsboard.server.common.data.yunteng.common.DeleteGroup;
29 29 import org.thingsboard.server.common.data.yunteng.core.exception.ThingsKitException;
  30 +import org.thingsboard.server.common.data.yunteng.core.exception.TkDataValidationException;
30 31 import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage;
31 32 import org.thingsboard.server.common.data.yunteng.dto.DeleteDTO;
32 33 import org.thingsboard.server.common.data.yunteng.dto.DeviceProfileDTO;
... ... @@ -37,8 +38,6 @@ import org.thingsboard.server.common.data.yunteng.enums.TkScriptFunctionType;
37 38 import org.thingsboard.server.common.data.yunteng.utils.tools.TkPageData;
38 39 import org.thingsboard.server.common.msg.queue.ServiceQueue;
39 40 import org.thingsboard.server.controller.BaseController;
40   -import org.thingsboard.server.dao.exception.DataValidationException;
41   -import org.thingsboard.server.dao.yunteng.service.TkDeviceProfileService;
42 41 import org.thingsboard.server.dao.yunteng.service.TkDeviceScriptService;
43 42 import org.thingsboard.server.transport.tcp.script.TkScriptInvokeService;
44 43
... ... @@ -60,7 +59,6 @@ public class TkDeviceScriptController extends BaseController {
60 59 private static final JsonParser parser = new JsonParser();
61 60 private static final ObjectMapper objectMapper = new ObjectMapper();
62 61 private final TkDeviceScriptService scriptService;
63   - private final TkDeviceProfileService ytDeviceProfileService;
64 62 private final TkScriptInvokeService jsEngine;
65 63
66 64 @PostMapping()
... ... @@ -215,18 +213,18 @@ public class TkDeviceScriptController extends BaseController {
215 213 public ResponseEntity<String> modbus(@RequestBody TkDeviceRpcDTO inputParams)
216 214 throws ThingsKitException {
217 215 if(StringUtils.isEmpty(inputParams.getDeviceCode())){
218   - throw new DataValidationException(String.format(ErrorMessage.NEED_MAIN_PARAMETER.getMessage(),"设备地址码"));
  216 + throw new TkDataValidationException(String.format(ErrorMessage.NEED_MAIN_PARAMETER.getMessage(),"设备地址码"));
219 217 }
220 218 if(StringUtils.isEmpty(inputParams.getMethod()) || null == inputParams.getCrc() ||
221 219 null ==inputParams.getRegisterAddress()){
222   - throw new DataValidationException(ErrorMessage.INVALID_PARAMETER.getMessage());
  220 + throw new TkDataValidationException(ErrorMessage.INVALID_PARAMETER.getMessage());
223 221 }
224 222 if(null !=inputParams.getRegisterNumber() && null !=inputParams.getRegisterValues() &&
225 223 !inputParams.getRegisterValues().isEmpty()){
226 224 List<Integer> registerValues = inputParams.getRegisterValues();
227 225 for (Integer value : registerValues){
228 226 if(null == value){
229   - throw new DataValidationException(ErrorMessage.INVALID_PARAMETER.getMessage());
  227 + throw new TkDataValidationException(ErrorMessage.REGISTER_VALUE_IS_NONE.getMessage());
230 228 }
231 229 }
232 230 }
... ...
... ... @@ -109,7 +109,8 @@ public enum ErrorMessage {
109 109 RESET_PASSWORD_SUCCESS(400085,"重置成功,请使用默认密码登录"),
110 110 HAVE_NO_PERMISSION(500002,"没有修改权限"),
111 111 MESSAGE_TEMPLATE_DELETED(400020,"消息模板不存在!"),
112   - NEED_MAIN_PARAMETER(400001, "缺少必要参数【%s】"),
  112 + NEED_MAIN_PARAMETER(400021, "缺少必要参数【%s】"),
  113 + REGISTER_VALUE_IS_NONE(400022, "寄存器值不能为空"),
113 114 NOT_ALLOED_ISOLATED_IN_MONOLITH(500003,"【monolith】模式下,不能选择【isolated】类型的租户配置"),
114 115 MESSAGE_TEMPLATE_USING_CONFIG(500004,"删除消息配置前,需要禁用使用消息配置的消息模板%s。");
115 116 private final int code;
... ...