1

将 pureconfig 与 Enumeratum 一起使用时,如何获得更好的异常? https://github.com/pureconfig/pureconfig/tree/master/modules/enumeratum

import pureconfig._
import pureconfig.generic.auto._
import pureconfig.module.enumeratum._
import com.typesafe.config.ConfigFactory.parseString
import enumeratum._
import enumeratum.EnumEntry._

object example {
  sealed trait Greeting extends EnumEntry with Snakecase

  object Greeting extends Enum[Greeting] {
    val values = findValues
    case object Hello extends Greeting
    case object GoodBye extends Greeting
    case object ShoutGoodBye extends Greeting with Uppercase
  }
}
import example._
case class GreetingConf(start: Greeting, end: Greeting)

val conf = parseString("""{
  start: hello
  end: SHOUT_GOOD_BYE
}""")
loadConfig[GreetingConf](conf)

但:

val conf = parseString("""{
      start: helloError
      end: SHOUT_GOOD_BYEFoo
    }""")
    loadConfig[GreetingConf](conf)

失败并出现令人讨厌的异常。而是类似于以下内容的描述性消息:

Can't parse key start to ENUM type Greeting. Only [hello, SHOUT_GOOD_BYE] are valid values.

会很好。我怎样才能做到这一点?

4

0 回答 0