Skip to content

Commit 5202fcb

Browse files
committed
explicit exception
Signed-off-by: Attila Mészáros <[email protected]>
1 parent 3c2f5dd commit 5202fcb

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.javaoperatorsdk.operator.api.reconciler;
2+
3+
import io.javaoperatorsdk.operator.OperatorException;
4+
5+
public class NonComparableResourceVersionException extends OperatorException {
6+
7+
public NonComparableResourceVersionException(String message) {
8+
super(message);
9+
}
10+
}

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/PrimaryUpdateAndCacheUtils.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -454,11 +454,11 @@ public static <P extends HasMetadata> P addFinalizerWithSSA(
454454
public static int compareResourceVersions(String v1, String v2) {
455455
var v1Length = v1.length();
456456
if (v1Length == 0) {
457-
throw new IllegalArgumentException("Resource version (1) is empty");
457+
throw new NonComparableResourceVersionException("Resource version (1) is empty");
458458
}
459459
var v2Length = v2.length();
460460
if (v2Length == 0) {
461-
throw new IllegalArgumentException("Resource version (2) is empty");
461+
throw new NonComparableResourceVersionException("Resource version (1) is empty");
462462
}
463463
var maxLength = Math.max(v1Length, v2Length);
464464
boolean v1LeadingZero = true;
@@ -470,25 +470,27 @@ public static int compareResourceVersions(String v1, String v2) {
470470
char1 = v1.charAt(i);
471471
if (v1LeadingZero) {
472472
if (char1 == '0') {
473-
throw new IllegalArgumentException("Resource version (1) cannot begin with 0");
473+
throw new NonComparableResourceVersionException(
474+
"Resource version (1) cannot begin with 0");
474475
}
475476
v1LeadingZero = false;
476477
}
477478
if (!Character.isDigit(char1)) {
478-
throw new IllegalArgumentException(
479+
throw new NonComparableResourceVersionException(
479480
"Non numeric characters in resource version (1): " + char1);
480481
}
481482
}
482483
if (i < v2Length) {
483484
var char2 = v2.charAt(i);
484485
if (v2LeadingZero) {
485486
if (char2 == '0') {
486-
throw new IllegalArgumentException("Resource version (2) cannot begin with 0");
487+
throw new NonComparableResourceVersionException(
488+
"Resource version (2) cannot begin with 0");
487489
}
488490
v2LeadingZero = false;
489491
}
490492
if (!Character.isDigit(char2)) {
491-
throw new IllegalArgumentException(
493+
throw new NonComparableResourceVersionException(
492494
"Non numeric characters in resource version (2): " + char2);
493495
}
494496
if (char1 == 0) {

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/api/reconciler/PrimaryUpdateAndCacheUtilsTest.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,22 @@ public void compareResourceVersionsTest() {
191191
assertThat(compareResourceVersions("123", "2")).isPositive();
192192
assertThat(compareResourceVersions("3", "211")).isNegative();
193193

194-
assertThrows(IllegalArgumentException.class, () -> compareResourceVersions("aa", "22"));
195-
assertThrows(IllegalArgumentException.class, () -> compareResourceVersions("11", "ba"));
196-
assertThrows(IllegalArgumentException.class, () -> compareResourceVersions("", "22"));
197-
assertThrows(IllegalArgumentException.class, () -> compareResourceVersions("11", ""));
198-
assertThrows(IllegalArgumentException.class, () -> compareResourceVersions("01", "123"));
199-
assertThrows(IllegalArgumentException.class, () -> compareResourceVersions("123", "01"));
200-
assertThrows(IllegalArgumentException.class, () -> compareResourceVersions("3213", "123a"));
201-
assertThrows(IllegalArgumentException.class, () -> compareResourceVersions("321", "123a"));
194+
assertThrows(
195+
NonComparableResourceVersionException.class, () -> compareResourceVersions("aa", "22"));
196+
assertThrows(
197+
NonComparableResourceVersionException.class, () -> compareResourceVersions("11", "ba"));
198+
assertThrows(
199+
NonComparableResourceVersionException.class, () -> compareResourceVersions("", "22"));
200+
assertThrows(
201+
NonComparableResourceVersionException.class, () -> compareResourceVersions("11", ""));
202+
assertThrows(
203+
NonComparableResourceVersionException.class, () -> compareResourceVersions("01", "123"));
204+
assertThrows(
205+
NonComparableResourceVersionException.class, () -> compareResourceVersions("123", "01"));
206+
assertThrows(
207+
NonComparableResourceVersionException.class, () -> compareResourceVersions("3213", "123a"));
208+
assertThrows(
209+
NonComparableResourceVersionException.class, () -> compareResourceVersions("321", "123a"));
202210
}
203211

204212
// naive performance test that compares the work case scenario for the parsing and non-parsing

0 commit comments

Comments
 (0)