-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #472 from iRevive/sdk-common/autoconfigure-error-d…
…etails sdk-common: `AutoConfigure` - use config keys in the error message
- Loading branch information
Showing
7 changed files
with
237 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...ommon/src/test/scala/org/typelevel/otel4s/sdk/autoconfigure/AutoConfigureErrorSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright 2023 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.typelevel.otel4s.sdk.autoconfigure | ||
|
||
import munit.ScalaCheckSuite | ||
import org.scalacheck.Gen | ||
import org.scalacheck.Prop | ||
|
||
class AutoConfigureErrorSuite extends ScalaCheckSuite { | ||
|
||
test("use hint in the error message") { | ||
Prop.forAll(Gen.alphaNumStr, Gen.alphaNumStr) { (hint, reason) => | ||
val error = AutoConfigureError(hint, new Err(reason)) | ||
|
||
assertEquals( | ||
error.getMessage, | ||
s"Cannot autoconfigure [$hint]. Cause: $reason." | ||
) | ||
} | ||
} | ||
|
||
test("use hint and config keys in the error message") { | ||
Prop.forAll(Gen.alphaNumStr, Gen.alphaNumStr) { (hint, reason) => | ||
val config = Config.ofProps( | ||
Map( | ||
"a.b.c.d" -> "value1", | ||
"c" -> "value;for;key;3" | ||
) | ||
) | ||
|
||
val keys: Set[Config.Key[_]] = Set( | ||
Config.Key[String]("a.b.c.d"), | ||
Config.Key[Set[String]]("a1.b1.c.1.d3"), | ||
Config.Key[Map[String, String]]("c"), | ||
Config.Key[Double]("d") | ||
) | ||
|
||
val error = AutoConfigureError(hint, new Err(reason), keys, config) | ||
|
||
val expected = | ||
s"""Cannot autoconfigure [$hint]. | ||
|Cause: $reason. | ||
|Config: | ||
|1) `a.b.c.d` - value1 | ||
|2) `a1.b1.c.1.d3` - N/A | ||
|3) `c` - value;for;key;3 | ||
|4) `d` - N/A""".stripMargin | ||
|
||
assertEquals(error.getMessage, expected) | ||
} | ||
} | ||
|
||
private class Err(reason: String) extends RuntimeException(reason) | ||
|
||
} |
69 changes: 69 additions & 0 deletions
69
sdk/common/src/test/scala/org/typelevel/otel4s/sdk/autoconfigure/AutoConfigureSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright 2023 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.typelevel.otel4s.sdk.autoconfigure | ||
|
||
import cats.effect.IO | ||
import cats.effect.Resource | ||
import cats.syntax.either._ | ||
import munit.CatsEffectSuite | ||
|
||
import scala.util.control.NoStackTrace | ||
|
||
class AutoConfigureSuite extends CatsEffectSuite { | ||
|
||
test("use hint in the error, ignore empty set of keys") { | ||
val config = Config.ofProps(Map.empty) | ||
|
||
val io = | ||
alwaysFailing("Component", Set.empty).configure(config).use_.attempt | ||
|
||
io.map(_.leftMap(_.getMessage)) | ||
.assertEquals(Left(AutoConfigureError("Component", Err).getMessage)) | ||
} | ||
|
||
test("use hint in the error, add keys to the error message") { | ||
val config = Config.ofProps(Map.empty) | ||
val keys: Set[Config.Key[_]] = Set( | ||
Config.Key[String]("a"), | ||
Config.Key[Set[String]]("b"), | ||
Config.Key[Map[String, String]]("c"), | ||
Config.Key[Double]("d") | ||
) | ||
|
||
val io = alwaysFailing("Component", keys).configure(config).use_.attempt | ||
|
||
io.map(_.leftMap(_.getMessage)) | ||
.assertEquals( | ||
Left(AutoConfigureError("Component", Err, keys, config).getMessage) | ||
) | ||
} | ||
|
||
private def alwaysFailing( | ||
hint: String, | ||
configKeys: Set[Config.Key[_]] | ||
): AutoConfigure.WithHint[IO, String] = { | ||
new AutoConfigure.WithHint[IO, String](hint, configKeys) { | ||
protected def fromConfig(config: Config): Resource[IO, String] = | ||
Resource.raiseError(Err: Throwable) | ||
} | ||
} | ||
|
||
private object Err | ||
extends RuntimeException("Something went wrong") | ||
with NoStackTrace | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters