Skip to content

Commit

Permalink
Merge pull request #35 from austinarbor/improve-error-messages
Browse files Browse the repository at this point in the history
Improve Error Messaging
  • Loading branch information
austinarbor authored Sep 4, 2023
2 parents db4c956 + 57b6da4 commit 0e3f8d8
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 27 deletions.
2 changes: 2 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ignore:
- "**/exception/**"
6 changes: 0 additions & 6 deletions plugin/config/detekt-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@
<CurrentIssues>
<ID>EmptyFunctionBlock:VersionCatalogGeneratorPlugin.kt$VersionCatalogGeneratorPlugin${}</ID>
<ID>MagicNumber:LocalDependencyResolver.kt$LocalDependencyResolver$3</ID>
<ID>TooGenericExceptionThrown:FileCatalogParser.kt$FileCatalogParser$throw RuntimeException("${libraryName} not found in catalog file")</ID>
<ID>TooGenericExceptionThrown:FileCatalogParser.kt$FileCatalogParser$throw RuntimeException("Group not found ")</ID>
<ID>TooGenericExceptionThrown:FileCatalogParser.kt$FileCatalogParser$throw RuntimeException("Name not found")</ID>
<ID>TooGenericExceptionThrown:FileCatalogParser.kt$FileCatalogParser$throw RuntimeException("Version not found")</ID>
<ID>TooGenericExceptionThrown:FileCatalogParser.kt$FileCatalogParser$throw RuntimeException("Version ref '${it}' not found")</ID>
<ID>TooGenericExceptionThrown:GradleDependencyResolver.kt$GradleDependencyResolver$throw RuntimeException("Unable to resolve ${notation}")</ID>
<ID>TooGenericExceptionThrown:LocalDependencyResolver.kt$LocalDependencyResolver$throw RuntimeException( "LocalDependencyResolver can only resolve File, Path, or Dependency notations", )</ID>
<ID>TooGenericExceptionThrown:LocalDependencyResolver.kt$LocalDependencyResolver$throw RuntimeException("Path ${filePath} does not exist")</ID>
<ID>TooGenericExceptionThrown:LocalDependencyResolver.kt$LocalDependencyResolver$throw RuntimeException("Unable to parse notation '${notation}")</ID>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,14 @@ object Generator {
seenModules: MutableSet<String> = mutableSetOf(),
filter: (Dependency) -> Boolean,
): Map<String, List<Dependency>> {
return model.dependencyManagement.dependencies
val deps = model.dependencyManagement?.dependencies ?: listOf<Dependency>()
if (deps.isEmpty()) {
logger.warn(
"${model.groupId}:${model.artifactId}:${model.version} does not have any dependencies defined " +
"in dependencyManagement",
)
}
return deps
.asSequence()
.onEach { it.groupId = mapGroup(model, it.groupId) }
.filter(filter)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dev.aga.gradle.versioncatalogs.exception

class ConfigurationException : RuntimeException {
constructor() : super()

constructor(message: String) : super(message)

constructor(message: String, cause: Throwable) : super(message, cause)

constructor(cause: Throwable) : super(cause)

constructor(
message: String,
cause: Throwable,
enableSuppression: Boolean,
writableStackTrace: Boolean,
) : super(message, cause, enableSuppression, writableStackTrace)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dev.aga.gradle.versioncatalogs.exception

class ResolutionException : RuntimeException {
constructor() : super()

constructor(message: String) : super(message)

constructor(message: String, cause: Throwable) : super(message, cause)

constructor(cause: Throwable) : super(cause)

constructor(
message: String,
cause: Throwable,
enableSuppression: Boolean,
writableStackTrace: Boolean,
) : super(message, cause, enableSuppression, writableStackTrace)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.aga.gradle.versioncatalogs.service

import dev.aga.gradle.versioncatalogs.exception.ConfigurationException
import java.io.File
import org.apache.maven.model.Dependency
import org.tomlj.Toml
Expand All @@ -19,43 +20,58 @@ internal class FileCatalogParser(private val file: File) : CatalogParser {
?.let { it as? TomlTable }
?.let { it[libraryName] }
?.let { it as? TomlTable }
?: throw RuntimeException("${libraryName} not found in catalog file")
?: throw ConfigurationException(
"${libraryName} not found in catalog file ${file.absolutePath}",
)

val versions = toml["versions"]?.let { it as? TomlTable }

return getGAV(library, versions)
return getGAV(libraryName, library, versions)
}

private fun getGAV(library: TomlTable, versions: TomlTable?): Dependency {
private fun getGAV(libraryName: String, library: TomlTable, versions: TomlTable?): Dependency {
val (group, name) =
if (library["module"] is String) {
val split = (library["module"] as String).split(":")
split[0] to split[1]
} else {
val group =
library["group"]?.let { it as? String }
?: throw RuntimeException("Group not found ")
?: throw ConfigurationException(
"Group not found for library ${libraryName} in catalog file ${file.absolutePath}",
)
val name =
library["name"]?.let { it as? String }
?: throw RuntimeException("Name not found")
?: throw ConfigurationException(
"Name not found for library ${libraryName} in catalog file ${file.absolutePath}",
)
group to name
}

val version = getVersion(library, versions)
val version = getVersion(libraryName, library, versions)
return Dependency().apply {
groupId = group
artifactId = name
this.version = version
}
}

private fun getVersion(library: TomlTable, versions: TomlTable?): String {
library.getString("version.ref")?.let {
return versions?.getString(it)
?: throw RuntimeException("Version ref '${it}' not found")
private fun getVersion(libraryName: String, library: TomlTable, versions: TomlTable?): String {
if (library.isTable("version") && library.isString("version.ref")) {
val ref = library.getString("version.ref")
return versions?.getString(ref)
?: throw ConfigurationException(
"Version ref '${ref}' not found for library ${libraryName} in catalog file ${file.absolutePath}",
)
}

return library.getString("version") ?: throw RuntimeException("Version not found")
if (library.isString("version")) {
return library.getString("version")!!
}

throw ConfigurationException(
"Version not found for library ${libraryName} in catalog file ${file.absolutePath}",
)
}

private fun parseCatalog(file: File) = Toml.parse(file.toPath())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.aga.gradle.versioncatalogs.service

import dev.aga.gradle.versioncatalogs.exception.ResolutionException
import java.util.concurrent.atomic.AtomicInteger
import java.util.function.Supplier
import org.apache.maven.model.Model
Expand Down Expand Up @@ -38,7 +39,7 @@ class GradleDependencyResolver(
val resolver = LocalDependencyResolver(path.parent)
return resolver.resolve(path.fileName)
}
throw RuntimeException("Unable to resolve ${notation}")
throw ResolutionException("Unable to resolve ${notation}")
}

private fun createConfiguration(): Configuration {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.aga.gradle.versioncatalogs.service

import dev.aga.gradle.versioncatalogs.exception.ConfigurationException
import java.nio.file.Paths
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatExceptionOfType
Expand All @@ -11,7 +12,12 @@ import org.junit.jupiter.params.provider.MethodSource
internal class FileCatalogParserTest {
@ParameterizedTest
@MethodSource("testFindBomProvider")
fun testFindBom(libraryName: String, expected: Array<String>, shouldThrow: Boolean = false) {
fun testFindBom(
libraryName: String,
expected: Array<String>,
shouldThrow: Boolean,
errorContains: String,
) {
val file = buildPath("libs.versions.toml").toFile()
val parser = FileCatalogParser(file)
if (!shouldThrow) {
Expand All @@ -20,9 +26,9 @@ internal class FileCatalogParserTest {
.extracting("groupId", "artifactId", "version")
.containsExactly(*expected)
} else {
assertThatExceptionOfType(RuntimeException::class.java).isThrownBy {
parser.findLibrary(libraryName)
}
assertThatExceptionOfType(ConfigurationException::class.java)
.isThrownBy { parser.findLibrary(libraryName) }
.withMessageContaining(errorContains)
}
}

Expand All @@ -33,10 +39,26 @@ internal class FileCatalogParserTest {
@JvmStatic
private fun testFindBomProvider(): List<Arguments> {
return listOf(
arguments("groovy-core", arrayOf("org.codehaus.groovy", "groovy", "3.0.5"), false),
arguments("fake-lib", arrayOf("dev.aga.lib", "fake-lib", "1.0.2"), false),
arguments("another-lib", arrayOf("dev.aga.lib", "another-lib", "1.0.0"), false),
arguments("commons-lang3", arrayOf(""), true),
arguments(
"groovy-core",
arrayOf("org.codehaus.groovy", "groovy", "3.0.5"),
false,
"",
),
arguments("fake-lib", arrayOf("dev.aga.lib", "fake-lib", "1.0.2"), false, ""),
arguments("another-lib", arrayOf("dev.aga.lib", "another-lib", "1.0.0"), false, ""),
arguments(
"commons-lang3",
arrayOf(""),
true,
"Version not found for library commons-lang3 in catalog file",
),
arguments(
"missing-ref",
arrayOf(""),
true,
"Version ref 'bad-ref' not found for library missing-ref in catalog file",
),
)
}

Expand Down
2 changes: 2 additions & 0 deletions plugin/src/test/resources/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ groovy-core = { module = "org.codehaus.groovy:groovy", version.ref = "groovy" }
groovy-json = { module = "org.codehaus.groovy:groovy-json", version.ref = "groovy" }
groovy-nio = { module = "org.codehaus.groovy:groovy-nio", version.ref = "groovy" }
commons-lang3 = { group = "org.apache.commons", name = "commons-lang3", version = { strictly = "[3.8, 4.0[", prefer = "3.9" } }
missing-ref = { group = "org.apache.commons", name = "commons-lang3", version.ref = "bad-ref" }
fake-lib = { group = "dev.aga.lib", name = "fake-lib", version = "1.0.2" }
another-lib = { group = "dev.aga.lib", name = "another-lib", version.ref = "dev" }


[bundles]
groovy = ["groovy-core", "groovy-json", "groovy-nio"]

Expand Down

0 comments on commit 0e3f8d8

Please sign in to comment.