Skip to content

Commit

Permalink
#354 Add better yaml errors when invalid file
Browse files Browse the repository at this point in the history
  • Loading branch information
sksamuel committed Mar 22, 2023
1 parent 07cb4cc commit 1160c4b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ sealed interface ConfigFailure {
"Expected args are ${constructor.parameters.map { it.type.simpleName }}. Underlying error was $e"
}

data class PropertySourceFailure(val msg: String) : ConfigFailure {
override fun description(): String = msg
data class PropertySourceFailure(val msg: String, val cause: Throwable?) : ConfigFailure {
override fun description(): String = msg + " " + cause
}

data class DataClassWithoutConstructor(val kclass: KClass<*>) : ConfigFailure {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ConfigFilePropertySource(
input.available() == 0 -> ConfigFailure.EmptyConfigSource(config).invalid()
else -> runCatching {
input.use { parser.load(it, config.describe()) }
}.toValidated { ConfigFailure.PropertySourceFailure("Could not parse ${config.describe()}") }
}.toValidated { ConfigFailure.PropertySourceFailure("Could not parse ${config.describe()}", it) }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.sksamuel.hoplite.yaml

import com.sksamuel.hoplite.ConfigLoaderBuilder
import com.sksamuel.hoplite.PropertySource
import io.kotest.assertions.throwables.shouldThrowAny
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.string.shouldInclude

class ParseErrorTest : FunSpec() {
init {
test("foo") {
shouldThrowAny {
ConfigLoaderBuilder.default()
.addSource(PropertySource.resource("/bad_indent.yml"))
.build()
.loadNodeOrThrow()
}.message shouldInclude """while parsing a block mapping
in 'reader', line 22, column 5"""
}
}
}
30 changes: 30 additions & 0 deletions hoplite-yaml/src/test/resources/bad_indent.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
ort:
allowedProcessEnvironmentVariableNames:
- PASSPORT
- USER_HOME
deniedProcessEnvironmentVariablesSubstrings:
- PASS
- SECRET
- TOKEN
- USER
enableRepositoryPackageCurations: true
enableRepositoryPackageConfigurations: true

licenseFilePatterns:
licenseFilenames: [ 'license*' ]
patentFilenames: [ patents ]
rootLicenseFilenames: [ 'readme*' ]

severeIssueThreshold: ERROR
severeRuleViolationThreshold: ERROR

analyzer:
allowDynamicVersions: true
enabledPackageManagers: [ Gradle, Yarn ]
packageManagers:
Yarn:
options:
directDependenciesOnly: true
Gradle:
options:
directDependenciesOnly: true

0 comments on commit 1160c4b

Please sign in to comment.