Skip to content

Commit

Permalink
improve error handling when a url/homepage is invalid
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
raboof committed Nov 11, 2024
1 parent 1883df7 commit 0f882e1
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/main/scala/sbtlicensereport/license/LicenseReport.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package sbtlicensereport
package license

import java.net.URISyntaxException

import sbt._
import sbt.io.Using
import sbt.internal.librarymanagement.IvySbt
Expand Down Expand Up @@ -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))
Expand All @@ -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),
Expand Down Expand Up @@ -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
}

Expand All @@ -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 {
Expand Down

0 comments on commit 0f882e1

Please sign in to comment.