• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pureconfig / pureconfig / 16844894943

09 Aug 2025 03:28AM UTC coverage: 94.686% (-0.04%) from 94.726%
16844894943

Pull #1838

web-flow
Merge 12f0062cf into 38f73c635
Pull Request #1838: Rid of deprecated URL constructor

1 of 1 new or added line in 1 file covered. (100.0%)

95 existing lines in 26 files now uncovered.

2744 of 2898 relevant lines covered (94.69%)

2.43 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/core/src/main/scala/pureconfig/error/FailureReason.scala
1
package pureconfig.error
2

3
import scala.annotation.tailrec
4
import scala.collection.mutable
5

6
import com.typesafe.config.{ConfigValue, ConfigValueType}
7

8
/** A representation of a reason why a value failed to be converted.
9
  */
10
trait FailureReason {
11

12
  /** A human-readable description of the failure.
13
    */
14
  def description: String
15
}
16

17
/** A general reason given for the failure of a value to be converted to a desired type.
18
  *
19
  * @param value
20
  *   the value that was requested to be converted
21
  * @param toType
22
  *   the target type that the value was requested to be converted to
23
  * @param because
24
  *   the reason why the conversion was not possible
25
  */
26
final case class CannotConvert(value: String, toType: String, because: String) extends FailureReason {
UNCOV
27
  def description = s"Cannot convert '$value' to $toType: $because."
28
}
29

30
/** A failure reason given when there is a collision of keys with different semantics. This error is raised when a key
31
  * that should be used to disambiguate a coproduct is mapped to a field in a product.
32
  *
33
  * @param key
34
  *   the colliding key
35
  * @param existingValue
36
  *   the value of the key
37
  */
38
final case class CollidingKeys(key: String, existingValue: ConfigValue) extends FailureReason {
UNCOV
39
  def description =
UNCOV
40
    s"Key with value '{$existingValue.render(ConfigRenderOptions.concise)}' collides with a key necessary to disambiguate a coproduct."
41
}
42

43
/** A failure reason given when a key is missing from a `ConfigObject` or `ConfigList`.
44
  *
45
  * @param key
46
  *   the key that is missing
47
  * @param candidates
48
  *   a set of candidate keys that might correspond to the desired key in case of a misconfigured ProductHint
49
  */
50
final case class KeyNotFound(key: String, candidates: Set[String] = Set()) extends FailureReason {
1✔
51
  def description = {
1✔
52
    if (candidates.nonEmpty) {
3✔
53
      val descLines = mutable.ListBuffer[String]()
3✔
54
      descLines += s"Key not found: '$key'. You might have a misconfigured ProductHint, since the following similar keys were found:"
3✔
55
      candidates.foreach { candidate =>
3✔
56
        descLines += s"  - '$candidate'"
3✔
57
      }
58
      descLines.mkString("\n")
3✔
59
    } else {
1✔
60
      s"Key not found: '$key'."
3✔
61
    }
62
  }
63
}
64

65
object KeyNotFound {
66
  @tailrec
67
  private[this] def isSubsequence(s1: String, s2: String): Boolean = {
1✔
68
    if (s1.isEmpty)
7✔
69
      true
4✔
70
    else if (s2.isEmpty)
7✔
71
      false
7✔
72
    else if (s1.head == s2.head)
7✔
73
      isSubsequence(s1.tail, s2.tail)
6✔
74
    else
75
      isSubsequence(s1, s2.tail)
7✔
76
  }
77

78
  def forKeys(fieldName: String, keys: Iterable[String]): KeyNotFound = {
1✔
79
    val lcField = fieldName.toLowerCase.filter(c => c.isDigit || c.isLetter)
8✔
80
    val objectKeys = keys.map(f => (f, f.toLowerCase))
8✔
81
    val candidateKeys = objectKeys.filter(k => isSubsequence(lcField, k._2)).map(_._1).toSet
7✔
82
    KeyNotFound(fieldName, candidateKeys)
7✔
83
  }
84
}
85

86
/** A failure reason given when an unknown key is found in a `ConfigObject`. The failure is raised when a key of a
87
  * `ConfigObject` is not mapped into a field of a given type and the `allowUnknownKeys` property of the `ProductHint`
88
  * for the type in question is `false`.
89
  *
90
  * @param key
91
  *   the unknown key
92
  */
93
final case class UnknownKey(key: String) extends FailureReason {
94
  def description = s"Unknown key."
1✔
95
}
96

97
/** A failure reason given when a `ConfigValue` has the wrong type.
98
  *
99
  * @param foundType
100
  *   the `ConfigValueType` that was found
101
  * @param expectedTypes
102
  *   the `ConfigValueType`s that were expected
103
  */
104
final case class WrongType(foundType: ConfigValueType, expectedTypes: Set[ConfigValueType]) extends FailureReason {
105
  def description = s"""Expected type ${expectedTypes.mkString(" or ")}. Found $foundType instead."""
1✔
106
}
107

108
/** A failure reason given when an exception is thrown during a conversion.
109
  *
110
  * @param throwable
111
  *   the `Throwable` that was raised
112
  */
113
final case class ExceptionThrown(throwable: Throwable) extends FailureReason {
UNCOV
114
  def description = s"${throwable.getMessage}."
115
}
116

117
/** A failure reason given when an unexpected empty string is found.
118
  *
119
  * @param typ
120
  *   the type that was attempted to be converted to from an empty string
121
  */
122
final case class EmptyStringFound(typ: String) extends FailureReason {
UNCOV
123
  def description = s"Empty string found when trying to convert to $typ."
124
}
125

126
/** A failure reason given when a list of an unexpected size is found when attempting to read into an `HList`.
127
  *
128
  * @param expected
129
  *   the expected number of elements
130
  * @param found
131
  *   the number of elements found
132
  */
133
final case class WrongSizeList(expected: Int, found: Int) extends FailureReason {
134
  def description = s"List of wrong size found. Expected $expected elements. Found $found elements instead."
1✔
135
}
136

137
/** A failure reason given when a string is not of the expected size.
138
  *
139
  * @param expected
140
  *   the expected number of characters
141
  * @param found
142
  *   the number of characters found
143
  */
144
final case class WrongSizeString(expected: Int, found: Int) extends FailureReason {
UNCOV
145
  def description = s"String of wrong size found. Expected $expected characters. Found $found characters instead."
146
}
147

148
/** A failure reason given when a user validation failed.
149
  *
150
  * @param description
151
  *   the validation failed description
152
  */
153
final case class UserValidationFailed(description: String) extends FailureReason
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc