Skip to content

Commit

Permalink
Merge pull request #132 from jenkinsci/undefined-metric
Browse files Browse the repository at this point in the history
Improve exception handling if metric is missing
  • Loading branch information
uhafner authored Nov 3, 2024
2 parents 4406f87 + 1759fff commit 4f5a182
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/main/java/edu/hm/hafner/coverage/Metric.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.util.TreeSet;
import java.util.stream.Stream;

import org.apache.commons.lang3.StringUtils;

import com.google.errorprone.annotations.Immutable;

import edu.hm.hafner.coverage.Coverage.CoverageBuilder;
Expand Down Expand Up @@ -79,7 +81,10 @@ public static Metric fromName(final String name) {
return metric;
}
}
throw new IllegalArgumentException("No metric found for name: " + name);
if (StringUtils.isBlank(name)) {
throw new IllegalArgumentException("No metric defined");
}
throw new IllegalArgumentException("No metric found for name '" + name + "'");
}

private static String normalize(final String name) {
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/edu/hm/hafner/coverage/MetricTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ void shouldMapFromName(final String name) {
assertThat(Metric.fromName(name)).isSameAs(Metric.CYCLOMATIC_COMPLEXITY);
}

@Test
void shouldProvideContextWhenMetricIsWrong() {
assertThatIllegalArgumentException().isThrownBy(() -> Metric.fromName("undefined"))
.withMessageContaining("No metric found for name 'undefined'");
assertThatIllegalArgumentException().isThrownBy(() -> Metric.fromName(""))
.withMessageContaining("No metric defined");
}

@EnumSource(Metric.class)
@ParameterizedTest(name = "{0} should be converted to a tag name and then back to a metric")
void shouldConvertToTags(final Metric metric) {
Expand Down

0 comments on commit 4f5a182

Please sign in to comment.