From 0f882e1a99262f4f812d8036504d0d27a0650ff8 Mon Sep 17 00:00:00 2001 From: Arnout Engelen Date: Mon, 11 Nov 2024 11:45:21 +0100 Subject: [PATCH] improve error handling when a url/homepage is invalid Since #134, encountering an 'invalid' URI such as `https://github.com/rnorth/${project.artifactId}` from https://github.com/rnorth/duct-tape would throw an error and fail the entire job. As the user of the plugin likely doesn't have direct influence over the `pom.xml` of the dependencies, it would be better to emit a warning. With this change, it becomes: ``` sbt-license-report: dependency [org.rnorth.duct-tape:duct-tape:1.0.8] has malformed homepage url [https://github.com/rnorth/${project.artifactId}] ``` This improves the situation described in #144. I'm not sure it is a full fix, because I'm not sure `https://github.com/rnorth/${project.artifactId}` is actually invalid here: I haven't checked whether maven placeholders are allowed here, and if so, whose responsibility it is to make sure they get resolved. --- .../license/LicenseReport.scala | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/main/scala/sbtlicensereport/license/LicenseReport.scala b/src/main/scala/sbtlicensereport/license/LicenseReport.scala index 5b63ae7..f0f318a 100644 --- a/src/main/scala/sbtlicensereport/license/LicenseReport.scala +++ b/src/main/scala/sbtlicensereport/license/LicenseReport.scala @@ -1,6 +1,8 @@ package sbtlicensereport package license +import java.net.URISyntaxException + import sbt._ import sbt.io.Using import sbt.internal.librarymanagement.IvySbt @@ -171,7 +173,8 @@ object LicenseReport { dep: ModuleReport, configs: Set[String], categories: Seq[LicenseCategory], - originatingModule: DepModuleInfo + originatingModule: DepModuleInfo, + log: Logger ): Option[DepLicense] = { val cs = dep.configurations val filteredConfigs = if (cs.isEmpty) cs else cs.filter(configs.map(ConfigRef.apply)) @@ -180,7 +183,15 @@ object LicenseReport { None else { val licenses = dep.licenses - val homepage = dep.homepage.map(string => new URI(string).toURL) + val homepage = dep.homepage.flatMap(string => { + try { + Some(new URI(string).toURL) + } catch { + case _: URISyntaxException => + log.warn(s"sbt-license-report: dependency [${dep.module}] has malformed homepage url [$string]") + None + } + }) Some( DepLicense( getModuleInfo(dep), @@ -213,11 +224,12 @@ object LicenseReport { report: UpdateReport, configs: Set[String] = Set.empty, categories: Seq[LicenseCategory] = LicenseCategory.all, - originatingModule: DepModuleInfo + originatingModule: DepModuleInfo, + log: Logger ): Seq[DepLicense] = { for { dep <- allModuleReports(report.configurations) - report <- pickLicenseForDep(dep, configs, categories, originatingModule) + report <- pickLicenseForDep(dep, configs, categories, originatingModule, log) } yield report } @@ -230,7 +242,7 @@ object LicenseReport { originatingModule: DepModuleInfo, log: Logger ): LicenseReport = { - val licenses = getLicenses(report, configs, categories, originatingModule) filterNot { dep => + val licenses = getLicenses(report, configs, categories, originatingModule, log) filterNot { dep => exclusions(dep.module).getOrElse(false) } map { l => overrides(l.module) match {