mirror of
https://github.com/LamGC/ScalaBot.git
synced 2025-04-29 22:27:31 +00:00
refactor(config): 包装 Serializer 可能抛出的异常.
增加对 Serializer 中可能抛出的异常(例如 MalformedURLException, IllegalArgumentException)包装成 JsonParseException, 以避免异常类型混乱的问题.
This commit is contained in:
parent
d5e66156b9
commit
a2667438f2
@ -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<ProxyType>,
|
||||
@ -65,7 +66,12 @@ object ArtifactSerializer : JsonSerializer<Artifact>, JsonDeserializer<Artifact>
|
||||
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)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user