diff --git a/context/src/main/java/io/micronaut/runtime/converters/time/TimeConverterRegistrar.java b/context/src/main/java/io/micronaut/runtime/converters/time/TimeConverterRegistrar.java index 34445dcd18..a8128dc815 100644 --- a/context/src/main/java/io/micronaut/runtime/converters/time/TimeConverterRegistrar.java +++ b/context/src/main/java/io/micronaut/runtime/converters/time/TimeConverterRegistrar.java @@ -355,13 +355,19 @@ private BiFunction> duration } context.reject( value, - new DateTimeParseException("Unparseable date format (" + value + "). Should either be a ISO-8601 duration or a round number followed by the unit type", value, 0)); + new DateTimeParseException("Unparseable date format (" + value + "). Should either be a ISO-8601 duration or a round number followed by the unit type", value, 0) + ); return Optional.empty(); } } } catch (NumberFormatException e) { context.reject(value, e); } + } else { + context.reject( + value, + new DateTimeParseException("Unparseable date format (" + value + "). Should either be a ISO-8601 duration or a round number followed by the unit type", value, 0) + ); } } return Optional.empty(); @@ -401,13 +407,19 @@ private BiFunction> periodConv default -> { context.reject( value, - new DateTimeParseException("Unparseable date format (" + value + "). Should either be a ISO-8601 duration or a round number followed by the unit type", value, 0)); + new DateTimeParseException("Unparseable date format (" + value + "). Should either be a ISO-8601 duration or a round number followed by the unit type", value, 0) + ); return Optional.empty(); } } } catch (NumberFormatException e) { context.reject(value, e); } + } else { + context.reject( + value, + new DateTimeParseException("Unparseable date format (" + value + "). Should either be a ISO-8601 duration or a round number followed by the unit type", value, 0) + ); } } return Optional.empty(); diff --git a/context/src/test/groovy/io/micronaut/runtime/converters/time/TimeConverterConfigurationSpec.groovy b/context/src/test/groovy/io/micronaut/runtime/converters/time/TimeConverterConfigurationSpec.groovy new file mode 100644 index 0000000000..f36a13990f --- /dev/null +++ b/context/src/test/groovy/io/micronaut/runtime/converters/time/TimeConverterConfigurationSpec.groovy @@ -0,0 +1,43 @@ +package io.micronaut.runtime.converters.time + +import io.micronaut.context.ApplicationContext +import io.micronaut.context.annotation.Requires +import io.micronaut.context.annotation.Value +import io.micronaut.context.exceptions.DependencyInjectionException +import jakarta.inject.Singleton +import java.time.Duration +import spock.lang.Specification + +class TimeConverterConfigurationSpec extends Specification { + + void 'test exception message with incorrect duration value'() { + given: + ApplicationContext ctx = ApplicationContext.run([ + 'spec.name' : 'TimeConverterRegistrarSpec', + 'health-check-cache-expiration': '60 seconds', + ]) + + when: + var myTask = ctx.getBean(MyTask) + + then: + var e = thrown(DependencyInjectionException.class) + e.message.contains("Error resolving property value [\${health-check-cache-expiration}]. Unable to convert value [60 seconds] to target type [Duration] due to: Unparseable date format (60 seconds). Should either be a ISO-8601 duration or a round number followed by the unit type") + } + + @Singleton + @Requires(property = 'spec.name', value = 'TimeConverterRegistrarSpec') + static class MyTask { + + @Value("\${health-check-cache-expiration}") + private Duration healthCheckCacheExpiration; + + Duration getHealthCheckCacheExpiration() { + return healthCheckCacheExpiration + } + + void setHealthCheckCacheExpiration(Duration healthCheckCacheExpiration) { + this.healthCheckCacheExpiration = healthCheckCacheExpiration + } + } +}