Skip to content

Commit a427c96

Browse files
committed
Include type in error message
And extract checking/throwing code
1 parent 588a203 commit a427c96

File tree

3 files changed

+23
-41
lines changed

3 files changed

+23
-41
lines changed

algo/src/main/java/org/neo4j/gds/similarity/knn/metrics/NullCheckingNodeProperties.java

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020
package org.neo4j.gds.similarity.knn.metrics;
2121

22+
import org.jetbrains.annotations.Nullable;
2223
import org.neo4j.gds.api.IdMap;
2324
import org.neo4j.gds.api.NodeProperties;
2425
import org.neo4j.gds.api.nodeproperties.ValueType;
@@ -41,52 +42,28 @@ public class NullCheckingNodeProperties implements NodeProperties {
4142
@Override
4243
public double[] doubleArrayValue(long nodeId) {
4344
var value = properties.doubleArrayValue(nodeId);
44-
if (value == null) {
45-
throw new IllegalArgumentException(formatWithLocale(
46-
"Missing node property `%s` for node with id `%s`.",
47-
name,
48-
idMap.toOriginalNodeId(nodeId)
49-
));
50-
}
45+
check(nodeId, value);
5146
return value;
5247
}
5348

5449
@Override
5550
public float[] floatArrayValue(long nodeId) {
5651
var value = properties.floatArrayValue(nodeId);
57-
if (value == null) {
58-
throw new IllegalArgumentException(formatWithLocale(
59-
"Missing node property `%s` for node with id `%s`.",
60-
name,
61-
idMap.toOriginalNodeId(nodeId)
62-
));
63-
}
52+
check(nodeId, value);
6453
return value;
6554
}
6655

6756
@Override
6857
public long[] longArrayValue(long nodeId) {
6958
var value = properties.longArrayValue(nodeId);
70-
if (value == null) {
71-
throw new IllegalArgumentException(formatWithLocale(
72-
"Missing node property `%s` for node with id `%s`.",
73-
name,
74-
idMap.toOriginalNodeId(nodeId)
75-
));
76-
}
59+
check(nodeId, value);
7760
return value;
7861
}
7962

8063
@Override
8164
public Object getObject(long nodeId) {
8265
var value = properties.getObject(nodeId);
83-
if (value == null) {
84-
throw new IllegalArgumentException(formatWithLocale(
85-
"Missing node property `%s` for node with id `%s`.",
86-
name,
87-
idMap.toOriginalNodeId(nodeId)
88-
));
89-
}
66+
check(nodeId, value);
9067
return value;
9168
}
9269

@@ -98,18 +75,23 @@ public ValueType valueType() {
9875
@Override
9976
public Value value(long nodeId) {
10077
var value = properties.value(nodeId);
101-
if (value == null) {
102-
throw new IllegalArgumentException(formatWithLocale(
103-
"Missing node property `%s` for node with id `%s`.",
104-
name,
105-
idMap.toOriginalNodeId(nodeId)
106-
));
107-
}
78+
check(nodeId, value);
10879
return value;
10980
}
11081

11182
@Override
11283
public long size() {
11384
return properties.size();
11485
}
86+
87+
private void check(long nodeId, @Nullable Object value) {
88+
if (value == null) {
89+
throw new IllegalArgumentException(formatWithLocale(
90+
"Missing `%s` node property `%s` for node with id `%s`.",
91+
properties.valueType().cypherName(),
92+
name,
93+
idMap.toOriginalNodeId(nodeId)
94+
));
95+
}
96+
}
11597
}

algo/src/test/java/org/neo4j/gds/similarity/knn/metrics/NullCheckingNodePropertiesTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void shouldThrowUsefullyForNullDoubleArrayValues() {
8282
var nonNullProps = new NullCheckingNodeProperties(nullProps, "propertyName", graph);
8383
assertThatThrownBy(() -> nonNullProps.doubleArrayValue(1))
8484
.isInstanceOf(IllegalArgumentException.class)
85-
.hasMessageContaining("Missing node property `propertyName` for node with id `1`.");
85+
.hasMessageContaining("Missing `List of Float` node property `propertyName` for node with id `1`.");
8686
}
8787

8888
@Test
@@ -91,7 +91,7 @@ void shouldThrowUsefullyForNullFloatArrayValues() {
9191
var nonNullProps = new NullCheckingNodeProperties(nullProps, "propertyName", graph);
9292
assertThatThrownBy(() -> nonNullProps.floatArrayValue(1))
9393
.isInstanceOf(IllegalArgumentException.class)
94-
.hasMessageContaining("Missing node property `propertyName` for node with id `1`.");
94+
.hasMessageContaining("Missing `List of Float` node property `propertyName` for node with id `1`.");
9595
}
9696

9797
@Test
@@ -100,6 +100,6 @@ void shouldThrowUsefullyForNullLongArrayValues() {
100100
var nonNullProps = new NullCheckingNodeProperties(nullProps, "propertyName", graph);
101101
assertThatThrownBy(() -> nonNullProps.longArrayValue(1))
102102
.isInstanceOf(IllegalArgumentException.class)
103-
.hasMessageContaining("Missing node property `propertyName` for node with id `1`.");
103+
.hasMessageContaining("Missing `List of Integer` node property `propertyName` for node with id `1`.");
104104
}
105105
}

algo/src/test/java/org/neo4j/gds/similarity/knn/metrics/SimilarityComputerTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ void doubleArraySimilarityComputerHandlesNullProperties() {
238238
);
239239
assertThatThrownBy(() -> sim.similarity(0, 1))
240240
.isInstanceOf(IllegalArgumentException.class)
241-
.hasMessageContaining("Missing node property `doubleArrayProperty` for node with id");
241+
.hasMessageContaining("Missing `List of Float` node property `doubleArrayProperty` for node with id");
242242
}
243243

244244
@Test
@@ -252,7 +252,7 @@ void floatArraySimilarityComputerHandlesNullProperties() {
252252
);
253253
assertThatThrownBy(() -> sim.similarity(0, 1))
254254
.isInstanceOf(IllegalArgumentException.class)
255-
.hasMessageContaining("Missing node property `floatArrayProperty` for node with id");
255+
.hasMessageContaining("Missing `List of Float` node property `floatArrayProperty` for node with id");
256256
}
257257

258258
@Test
@@ -277,7 +277,7 @@ public long size() {
277277
props,
278278
SimilarityMetric.defaultMetricForType(ValueType.LONG_ARRAY)
279279
)).isInstanceOf(IllegalArgumentException.class)
280-
.hasMessageContaining("Missing node property `longArrayProperty` for node with id");
280+
.hasMessageContaining("Missing `List of Integer` node property `longArrayProperty` for node with id");
281281
}
282282

283283
static Stream<SimilarityComputer> nonFiniteSimilarities() {

0 commit comments

Comments
 (0)