Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve exception handling if metric is missing #132

Merged
merged 1 commit into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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