From 76e7b6c74899837f72f6fd3faab32e574143d6f5 Mon Sep 17 00:00:00 2001 From: Andrapyre <42009361+Andrapyre@users.noreply.github.com> Date: Fri, 10 May 2024 05:01:17 +0200 Subject: [PATCH] fixing broken default currency test --- .../test/scala/zio/schema/DefaultValueSpec.scala | 2 ++ .../src/main/scala/zio/schema/StandardType.scala | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/tests/shared/src/test/scala/zio/schema/DefaultValueSpec.scala b/tests/shared/src/test/scala/zio/schema/DefaultValueSpec.scala index 1ec636fe0..8e3cf542b 100644 --- a/tests/shared/src/test/scala/zio/schema/DefaultValueSpec.scala +++ b/tests/shared/src/test/scala/zio/schema/DefaultValueSpec.scala @@ -143,6 +143,8 @@ object DefaultValueSpec extends ZIOSpecDefault { ) }, test("Currency default value") { + java.util.Locale + .setDefault(java.util.Locale.US) //This is a workaround for the default locale not being set in the JVM in Github actions as of May, 2024. assert(Primitive(StandardType.CurrencyType).defaultValue)( isRight(isSubtype[java.util.Currency](anything)) ) diff --git a/zio-schema/jvm/src/main/scala/zio/schema/StandardType.scala b/zio-schema/jvm/src/main/scala/zio/schema/StandardType.scala index 4050c08d4..19721881f 100644 --- a/zio-schema/jvm/src/main/scala/zio/schema/StandardType.scala +++ b/zio-schema/jvm/src/main/scala/zio/schema/StandardType.scala @@ -169,10 +169,20 @@ object StandardType { override def tag: String = Tags.CURRENCY override def defaultValue: Either[String, java.util.Currency] = try { - Right(java.util.Currency.getInstance(java.util.Locale.getDefault())) + val currency = java.util.Currency.getInstance(java.util.Locale.getDefault()) + if (currency == null) { + Left( + "Could not get default currency. In most cases, this is because a currency could not be determined from the provided locale (e.g. when the locale is set to Antarctica). Please inspect the default locale to ensure that it is properly set and try again." + ) + } else { + Right(currency) + } + } catch { - case _: NullPointerException => throw new Exception("Could not get default currency. Default locale was null.") - case ex: Throwable => throw new Exception(s"Could not get default currency. ${ex.getMessage}") + case ex: Throwable => + Left( + s"Could not get default currency. In most cases, this is because the default locale was not set on the JVM. If this is the case, running java.util.Locale.setDefault() before getting the default currency should fix this. Error message: ${ex.getMessage}." + ) } override def compare(x: java.util.Currency, y: java.util.Currency): Int =