Commit 81952aa1a72eb18ee2ef2562774834d2c4fe865e

Authored by xp.Huang
1 parent e456de06

fix:调整异常捕获

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