From 819124daa98e13c0436a8514a9ce454ba21bedba Mon Sep 17 00:00:00 2001 From: Viktor Lovgren Date: Mon, 23 Oct 2023 02:14:38 +0200 Subject: [PATCH] Change ConfigException to indent messages after line breaks --- .../shared/src/main/scala/ciris/ConfigException.scala | 8 +++++++- .../shared/src/test/scala/ciris/ConfigExceptionSpec.scala | 5 +++-- modules/core/shared/src/test/scala/ciris/Generators.scala | 3 ++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/modules/core/shared/src/main/scala/ciris/ConfigException.scala b/modules/core/shared/src/main/scala/ciris/ConfigException.scala index fe6d4192..2da64f87 100644 --- a/modules/core/shared/src/main/scala/ciris/ConfigException.scala +++ b/modules/core/shared/src/main/scala/ciris/ConfigException.scala @@ -36,12 +36,18 @@ object ConfigException { private[ciris] final val entryLeading: String = "\n - " + private[ciris] final val entryNewLineLeading: String = + " " * (entryLeading.length - 1) + private[ciris] final val entryTrailing: String = "." + private[ciris] final def formatMessage(message: String): String = + message.replaceAll("\n", s"\n$entryNewLineLeading") + private[ciris] final def message(error: ConfigError): String = { val messages = - error.messages + error.messages.map(formatMessage) val builder = new java.lang.StringBuilder(messageLength(messages)) diff --git a/modules/core/shared/src/test/scala/ciris/ConfigExceptionSpec.scala b/modules/core/shared/src/test/scala/ciris/ConfigExceptionSpec.scala index c39a9442..fc8bf186 100644 --- a/modules/core/shared/src/test/scala/ciris/ConfigExceptionSpec.scala +++ b/modules/core/shared/src/test/scala/ciris/ConfigExceptionSpec.scala @@ -25,7 +25,7 @@ final class ConfigExceptionSpec extends DisciplineSuite with Generators { exception.getMessage val messages = - exception.error.messages + exception.error.messages.map(ConfigException.formatMessage) val withEntryTrailing = messages @@ -63,7 +63,8 @@ final class ConfigExceptionSpec extends DisciplineSuite with Generators { property("ConfigException.messageLength") { forAll { (exception: ConfigException) => - val expected = ConfigException.messageLength(exception.error.messages) + val messages = exception.error.messages.map(ConfigException.formatMessage) + val expected = ConfigException.messageLength(messages) val actual = exception.getMessage.length actual === expected } diff --git a/modules/core/shared/src/test/scala/ciris/Generators.scala b/modules/core/shared/src/test/scala/ciris/Generators.scala index 4baef64f..51592eac 100644 --- a/modules/core/shared/src/test/scala/ciris/Generators.scala +++ b/modules/core/shared/src/test/scala/ciris/Generators.scala @@ -40,7 +40,8 @@ trait Generators extends GeneratorsRuntimePlatform { val configErrorMessageGen: Gen[String] = Gen.oneOf( Gen.alphaNumStr, - Gen.alphaNumStr.map(_ ++ ConfigException.entryTrailing) + Gen.alphaNumStr.map(_ ++ ConfigException.entryTrailing), + Gen.alphaNumStr.map(s => s"$s\n$s") ) val configErrorGen: Gen[ConfigError] =