Skip to content

Commit

Permalink
Add correct message, when set duration / period value in wrong format. (
Browse files Browse the repository at this point in the history
#11387)

Fixed #11385
  • Loading branch information
altro3 authored Dec 2, 2024
1 parent c15d72e commit 015e566
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -355,13 +355,19 @@ private BiFunction<CharSequence, ConversionContext, Optional<Duration>> 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();
Expand Down Expand Up @@ -401,13 +407,19 @@ private BiFunction<CharSequence, ConversionContext, Optional<Period>> 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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import io.micronaut.context.ApplicationContext;
import io.micronaut.context.LifeCycle;
import io.micronaut.context.exceptions.NoSuchBeanException;
import io.micronaut.context.processor.ExecutableMethodProcessor;
import io.micronaut.core.annotation.AnnotationMetadata;
import io.micronaut.core.annotation.Internal;
Expand Down

0 comments on commit 015e566

Please sign in to comment.