Browse Source

refactor(iot-hub): unify install/update parse-failure messages

Route all parse-failure throws through a single parseFailure(action, itemType[, section], cause) helper so the user-facing wording lives in one place and update paths share the template with install paths.
pull/15787/head
Igor Kulikov 2 weeks ago
parent
commit
2f118df5fd
  1. 42
      application/src/main/java/org/thingsboard/server/service/iot_hub/DefaultIotHubService.java

42
application/src/main/java/org/thingsboard/server/service/iot_hub/DefaultIotHubService.java

@ -81,6 +81,17 @@ import java.util.UUID;
@Slf4j @Slf4j
public class DefaultIotHubService implements IotHubService { public class DefaultIotHubService implements IotHubService {
private static final String ACTION_INSTALL = "install";
private static final String ACTION_UPDATE = "update";
private static final String SECTION_PACKAGE_DATA = "package data";
private static final String SECTION_RULE_CHAIN_METADATA = "rule chain metadata";
private static final String ITEM_TYPE_WIDGET = "widget";
private static final String ITEM_TYPE_DASHBOARD = "dashboard";
private static final String ITEM_TYPE_CALCULATED_FIELD = "calculated field";
private static final String ITEM_TYPE_RULE_CHAIN = "rule chain";
private static final String ITEM_TYPE_DEVICE_PROFILE = "device profile";
private final IotHubRestClient iotHubRestClient; private final IotHubRestClient iotHubRestClient;
private final TbWidgetTypeService tbWidgetTypeService; private final TbWidgetTypeService tbWidgetTypeService;
private final TbDashboardService tbDashboardService; private final TbDashboardService tbDashboardService;
@ -155,7 +166,7 @@ public class DefaultIotHubService implements IotHubService {
try { try {
widgetTypeDetails = JacksonUtil.fromString(new String(fileData), WidgetTypeDetails.class, true); widgetTypeDetails = JacksonUtil.fromString(new String(fileData), WidgetTypeDetails.class, true);
} catch (Exception e) { } catch (Exception e) {
throw new Exception("Failed to parse widget data: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()), e); throw parseFailure(ACTION_INSTALL, ITEM_TYPE_WIDGET, e);
} }
widgetTypeDetails.setId(null); widgetTypeDetails.setId(null);
widgetTypeDetails.setTenantId(tenantId); widgetTypeDetails.setTenantId(tenantId);
@ -171,7 +182,7 @@ public class DefaultIotHubService implements IotHubService {
try { try {
dashboard = JacksonUtil.fromString(new String(fileData), Dashboard.class, true); dashboard = JacksonUtil.fromString(new String(fileData), Dashboard.class, true);
} catch (Exception e) { } catch (Exception e) {
throw new Exception("Failed to parse dashboard data: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()), e); throw parseFailure(ACTION_INSTALL, ITEM_TYPE_DASHBOARD, e);
} }
dashboard.setId(null); dashboard.setId(null);
dashboard.setTenantId(tenantId); dashboard.setTenantId(tenantId);
@ -187,7 +198,7 @@ public class DefaultIotHubService implements IotHubService {
try { try {
calculatedField = JacksonUtil.fromString(new String(fileData), CalculatedField.class, true); calculatedField = JacksonUtil.fromString(new String(fileData), CalculatedField.class, true);
} catch (Exception e) { } catch (Exception e) {
throw new Exception("Failed to parse calculated field data: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()), e); throw parseFailure(ACTION_INSTALL, ITEM_TYPE_CALCULATED_FIELD, e);
} }
calculatedField.setId(null); calculatedField.setId(null);
calculatedField.setTenantId(tenantId); calculatedField.setTenantId(tenantId);
@ -211,14 +222,14 @@ public class DefaultIotHubService implements IotHubService {
try { try {
ruleChain = JacksonUtil.fromString(json.get("ruleChain").toString(), RuleChain.class, true); ruleChain = JacksonUtil.fromString(json.get("ruleChain").toString(), RuleChain.class, true);
} catch (Exception e) { } catch (Exception e) {
throw new Exception("Failed to parse rule chain: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()), e); throw parseFailure(ACTION_INSTALL, ITEM_TYPE_RULE_CHAIN, e);
} }
RuleChainMetaData metadata; RuleChainMetaData metadata;
try { try {
metadata = JacksonUtil.fromString(json.get("metadata").toString(), RuleChainMetaData.class, true); metadata = JacksonUtil.fromString(json.get("metadata").toString(), RuleChainMetaData.class, true);
} catch (Exception e) { } catch (Exception e) {
throw new Exception("Failed to parse rule chain metadata: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()), e); throw parseFailure(ACTION_INSTALL, ITEM_TYPE_RULE_CHAIN, SECTION_RULE_CHAIN_METADATA, e);
} }
ruleChain.setId(null); ruleChain.setId(null);
@ -281,7 +292,7 @@ public class DefaultIotHubService implements IotHubService {
try { try {
deviceProfile = JacksonUtil.fromString(new String(fileData), DeviceProfile.class, true); deviceProfile = JacksonUtil.fromString(new String(fileData), DeviceProfile.class, true);
} catch (Exception e) { } catch (Exception e) {
throw new Exception("Failed to parse device profile data: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()), e); throw parseFailure(ACTION_INSTALL, ITEM_TYPE_DEVICE_PROFILE, e);
} }
deviceProfile.setId(null); deviceProfile.setId(null);
deviceProfile.setTenantId(tenantId); deviceProfile.setTenantId(tenantId);
@ -405,7 +416,7 @@ public class DefaultIotHubService implements IotHubService {
try { try {
newWidgetType = JacksonUtil.fromString(new String(fileData), WidgetTypeDetails.class, true); newWidgetType = JacksonUtil.fromString(new String(fileData), WidgetTypeDetails.class, true);
} catch (Exception e) { } catch (Exception e) {
throw new Exception("Failed to parse widget data: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()), e); throw parseFailure(ACTION_UPDATE, ITEM_TYPE_WIDGET, e);
} }
WidgetTypeDetails existing = widgetTypeService.findWidgetTypeDetailsById(tenantId, descriptor.getWidgetTypeId()); WidgetTypeDetails existing = widgetTypeService.findWidgetTypeDetailsById(tenantId, descriptor.getWidgetTypeId());
if (existing == null) { if (existing == null) {
@ -421,7 +432,7 @@ public class DefaultIotHubService implements IotHubService {
try { try {
newDashboard = JacksonUtil.fromString(new String(fileData), Dashboard.class, true); newDashboard = JacksonUtil.fromString(new String(fileData), Dashboard.class, true);
} catch (Exception e) { } catch (Exception e) {
throw new Exception("Failed to parse dashboard data: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()), e); throw parseFailure(ACTION_UPDATE, ITEM_TYPE_DASHBOARD, e);
} }
Dashboard existing = dashboardService.findDashboardById(tenantId, descriptor.getDashboardId()); Dashboard existing = dashboardService.findDashboardById(tenantId, descriptor.getDashboardId());
if (existing == null) { if (existing == null) {
@ -437,7 +448,7 @@ public class DefaultIotHubService implements IotHubService {
try { try {
newCf = JacksonUtil.fromString(new String(fileData), CalculatedField.class, true); newCf = JacksonUtil.fromString(new String(fileData), CalculatedField.class, true);
} catch (Exception e) { } catch (Exception e) {
throw new Exception("Failed to parse calculated field data: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()), e); throw parseFailure(ACTION_UPDATE, ITEM_TYPE_CALCULATED_FIELD, e);
} }
CalculatedField existing = calculatedFieldService.findById(tenantId, descriptor.getCalculatedFieldId()); CalculatedField existing = calculatedFieldService.findById(tenantId, descriptor.getCalculatedFieldId());
if (existing == null) { if (existing == null) {
@ -455,7 +466,7 @@ public class DefaultIotHubService implements IotHubService {
try { try {
metadata = JacksonUtil.fromString(json.get("metadata").toString(), RuleChainMetaData.class, true); metadata = JacksonUtil.fromString(json.get("metadata").toString(), RuleChainMetaData.class, true);
} catch (Exception e) { } catch (Exception e) {
throw new Exception("Failed to parse rule chain metadata: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()), e); throw parseFailure(ACTION_UPDATE, ITEM_TYPE_RULE_CHAIN, SECTION_RULE_CHAIN_METADATA, e);
} }
RuleChain existing = ruleChainService.findRuleChainById(tenantId, descriptor.getRuleChainId()); RuleChain existing = ruleChainService.findRuleChainById(tenantId, descriptor.getRuleChainId());
if (existing == null) { if (existing == null) {
@ -465,7 +476,7 @@ public class DefaultIotHubService implements IotHubService {
try { try {
newRuleChain = JacksonUtil.fromString(json.get("ruleChain").toString(), RuleChain.class, true); newRuleChain = JacksonUtil.fromString(json.get("ruleChain").toString(), RuleChain.class, true);
} catch (Exception e) { } catch (Exception e) {
throw new Exception("Failed to parse rule chain: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()), e); throw parseFailure(ACTION_UPDATE, ITEM_TYPE_RULE_CHAIN, e);
} }
existing.setName(newRuleChain.getName()); existing.setName(newRuleChain.getName());
RuleChain savedRuleChain = ruleChainService.saveRuleChain(existing); RuleChain savedRuleChain = ruleChainService.saveRuleChain(existing);
@ -673,4 +684,13 @@ public class DefaultIotHubService implements IotHubService {
iotHubInstalledItemService.deleteById(tenantId, installedItemId); iotHubInstalledItemService.deleteById(tenantId, installedItemId);
log.info("[{}] Deleted installed IoT Hub item: {}", tenantId, installedItem.getItemName()); log.info("[{}] Deleted installed IoT Hub item: {}", tenantId, installedItem.getItemName());
} }
private static Exception parseFailure(String action, String itemTypeName, Exception cause) {
return parseFailure(action, itemTypeName, SECTION_PACKAGE_DATA, cause);
}
private static Exception parseFailure(String action, String itemTypeName, String section, Exception cause) {
return new Exception("Unable to " + action + " this " + itemTypeName + " — the " + section + " could not be parsed. " +
"The package may be corrupted or in an unexpected format; please contact the creator to have it re-published.", cause);
}
} }

Loading…
Cancel
Save