From 8162533179896a405bd9fd733d36c5f0db512aa9 Mon Sep 17 00:00:00 2001 From: wangchenjun Date: Fri, 6 Sep 2019 17:57:53 +0800 Subject: [PATCH] Make error information more specifically --- .../naming/controllers/InstanceController.java | 4 +--- .../com/alibaba/nacos/naming/core/Instance.java | 13 ++++++++----- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java index c8b4cb829..88b8c2c5e 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java @@ -317,9 +317,7 @@ public class InstanceController { instance.setMetadata(UtilsAndCommons.parseMetadata(metadata)); } - if (!instance.validate()) { - throw new NacosException(NacosException.INVALID_PARAM, "instance format invalid:" + instance); - } + instance.validate(); return instance; } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/Instance.java b/naming/src/main/java/com/alibaba/nacos/naming/core/Instance.java index c4612a66d..1e6419e11 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/Instance.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/Instance.java @@ -17,6 +17,7 @@ package com.alibaba.nacos.naming.core; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.annotation.JSONField; +import com.alibaba.nacos.naming.exception.NacosException; import com.alibaba.nacos.naming.healthcheck.HealthCheckStatus; import com.alibaba.nacos.naming.misc.Loggers; import com.alibaba.nacos.naming.misc.UtilsAndCommons; @@ -197,7 +198,9 @@ public class Instance extends com.alibaba.nacos.api.naming.pojo.Instance impleme ip.setWeight(0.0D); } - if (!ip.validate()) { + try { + ip.validate(); + } catch (NacosException e) { throw new IllegalArgumentException("malformed ip config: " + json); } @@ -298,19 +301,19 @@ public class Instance extends com.alibaba.nacos.api.naming.pojo.Instance impleme return getIp() + "#" + getPort() + "#" + getClusterName() + "#" + getServiceName(); } - public boolean validate() { + public void validate() throws NacosException { if (onlyContainsDigitAndDot()) { Matcher matcher = IP_PATTERN.matcher(getIp() + ":" + getPort()); if (!matcher.matches()) { - return false; + throw new NacosException(NacosException.INVALID_PARAM, "instance format invalid: Your IP address is spelled incorrectly"); } } if (getWeight() > MAX_WEIGHT_VALUE || getWeight() < MIN_WEIGHT_VALUE) { - return false; + throw new NacosException(NacosException.INVALID_PARAM, "instance format invalid: The weights range from " + + MIN_WEIGHT_VALUE + " to " + MAX_WEIGHT_VALUE); } - return true; } private boolean onlyContainsDigitAndDot() {