From a2667438f236b2be05e4ace2559e187e48db0780 Mon Sep 17 00:00:00 2001 From: LamGC Date: Thu, 23 Jun 2022 12:25:14 +0800 Subject: [PATCH] =?UTF-8?q?refactor(config):=20=E5=8C=85=E8=A3=85=20Serial?= =?UTF-8?q?izer=20=E5=8F=AF=E8=83=BD=E6=8A=9B=E5=87=BA=E7=9A=84=E5=BC=82?= =?UTF-8?q?=E5=B8=B8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加对 Serializer 中可能抛出的异常(例如 MalformedURLException, IllegalArgumentException)包装成 JsonParseException, 以避免异常类型混乱的问题. --- .../src/main/kotlin/serializer/Serializer.kt | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/scalabot-meta/src/main/kotlin/serializer/Serializer.kt b/scalabot-meta/src/main/kotlin/serializer/Serializer.kt index 37571af..d28e42e 100644 --- a/scalabot-meta/src/main/kotlin/serializer/Serializer.kt +++ b/scalabot-meta/src/main/kotlin/serializer/Serializer.kt @@ -11,6 +11,7 @@ import org.eclipse.aether.repository.Authentication import org.eclipse.aether.repository.Proxy import org.eclipse.aether.util.repository.AuthenticationBuilder import java.lang.reflect.Type +import java.net.MalformedURLException import java.net.URL object ProxyTypeSerializer : JsonDeserializer, @@ -65,7 +66,12 @@ object ArtifactSerializer : JsonSerializer, JsonDeserializer if (!json.isJsonPrimitive) { throw JsonParseException("Wrong configuration value type.") } - return DefaultArtifact(json.asString.trim()) + val artifactStr = json.asString.trim() + try { + return DefaultArtifact(artifactStr) + } catch (e: IllegalArgumentException) { + throw JsonParseException("Invalid artifact format: `${artifactStr}`.") + } } } @@ -124,10 +130,14 @@ object MavenRepositoryConfigSerializer ) } is JsonPrimitive -> { - MavenRepositoryConfig(url = URL(json.asString)) + try { + return MavenRepositoryConfig(url = URL(json.asString)) + } catch (e: MalformedURLException) { + throw JsonParseException("Invalid URL: ${json.asString}", e) + } } else -> { - throw JsonParseException("Unsupported Maven warehouse configuration type.") + throw JsonParseException("Unsupported Maven repository configuration type. (Only support JSON object or url string)") } } }