Skip to content

Commit aaf8e52

Browse files
committed
Introduce GraphNotFoundException in GraphStoreCatalog
1 parent 740dd63 commit aaf8e52

File tree

4 files changed

+66
-38
lines changed

4 files changed

+66
-38
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Neo4j is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
package org.neo4j.gds.core.loading;
21+
22+
import static org.neo4j.gds.utils.StringFormatting.formatWithLocale;
23+
24+
public class GraphNotFoundException extends RuntimeException {
25+
private final GraphStoreCatalog.UserCatalog.UserCatalogKey userCatalogKey;
26+
27+
GraphNotFoundException(GraphStoreCatalog.UserCatalog.UserCatalogKey userCatalogKey) {
28+
super(
29+
formatWithLocale(
30+
"Graph with name `%s` does not exist on database `%s`. It might exist on another database.",
31+
userCatalogKey.graphName(),
32+
userCatalogKey.databaseName()
33+
)
34+
);
35+
this.userCatalogKey = userCatalogKey;
36+
}
37+
38+
public String graphName() {
39+
return userCatalogKey.graphName();
40+
}
41+
42+
public String databaseName() {
43+
return userCatalogKey.databaseName();
44+
}
45+
}

core/src/main/java/org/neo4j/gds/core/loading/GraphStoreCatalog.java

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import java.util.HashSet;
4040
import java.util.Locale;
4141
import java.util.Map;
42-
import java.util.NoSuchElementException;
4342
import java.util.Optional;
4443
import java.util.concurrent.ConcurrentHashMap;
4544
import java.util.function.Consumer;
@@ -107,7 +106,7 @@ public static GraphStoreCatalogEntry get(CatalogRequest request, String graphNam
107106

108107
if (usersWithMatchingGraphs.isEmpty()) {
109108
// suggests only own graphs names
110-
throw ownCatalog.graphNotFoundException(userCatalogKey);
109+
throw new GraphNotFoundException(userCatalogKey);
111110
}
112111

113112
var usernames = StringJoining.joinVerbose(
@@ -151,7 +150,7 @@ public static void remove(
151150

152151
if (usersWithMatchingGraphs.isEmpty() && failOnMissing) {
153152
// suggests only own graphs names
154-
throw ownCatalog.graphNotFoundException(userCatalogKey);
153+
throw new GraphNotFoundException(userCatalogKey);
155154
}
156155

157156
if (usersWithMatchingGraphs.size() > 1) {
@@ -297,7 +296,8 @@ private static UserCatalog getUserCatalog(String username) {
297296
return userCatalogs.getOrDefault(username, UserCatalog.EMPTY);
298297
}
299298

300-
public record GraphStoreCatalogEntryWithUsername(GraphStoreCatalogEntry catalogEntry, String username) {}
299+
public record GraphStoreCatalogEntryWithUsername(GraphStoreCatalogEntry catalogEntry, String username) {
300+
}
301301

302302
static class UserCatalog {
303303

@@ -332,7 +332,11 @@ private void set(
332332
throw new IllegalArgumentException("Both name and graph store must be not null");
333333
}
334334

335-
GraphStoreCatalogEntry graphStoreCatalogEntry = new GraphStoreCatalogEntry(graphStore, config, new EphemeralResultStore());
335+
GraphStoreCatalogEntry graphStoreCatalogEntry = new GraphStoreCatalogEntry(
336+
graphStore,
337+
config,
338+
new EphemeralResultStore()
339+
);
336340

337341
if (graphsByName.containsKey(userCatalogKey)) {
338342
throw new IllegalStateException(
@@ -369,24 +373,13 @@ private void removeDegreeDistribution(UserCatalogKey userCatalogKey) {
369373
var graphStoreWithConfig = graphsByName.get(userCatalogKey);
370374

371375
if (graphStoreWithConfig == null && failOnMissing) {
372-
throw graphNotFoundException(userCatalogKey);
376+
GraphNotFoundException result;
377+
throw new GraphNotFoundException(userCatalogKey);
373378
}
374379

375380
return graphStoreWithConfig;
376381
}
377382

378-
private NoSuchElementException graphNotFoundException(UserCatalogKey userCatalogKey) {
379-
var graphName = userCatalogKey.graphName();
380-
381-
return new NoSuchElementException(
382-
formatWithLocale(
383-
"Graph with name `%s` does not exist on database `%s`. It might exist on another database.",
384-
graphName,
385-
userCatalogKey.databaseName()
386-
)
387-
);
388-
}
389-
390383
private Optional<Map<String, Object>> getDegreeDistribution(UserCatalogKey userCatalogKey) {
391384
if (!graphsByName.containsKey(userCatalogKey)) {
392385
return Optional.empty();

core/src/test/java/org/neo4j/gds/core/loading/GraphStoreCatalogServiceTest.java

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,11 @@
2929
import org.neo4j.gds.api.User;
3030
import org.neo4j.gds.config.GraphProjectConfig;
3131

32-
import java.util.NoSuchElementException;
33-
34-
import static org.assertj.core.api.Assertions.assertThat;
32+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
3533
import static org.junit.jupiter.api.Assertions.assertFalse;
3634
import static org.junit.jupiter.api.Assertions.assertNull;
3735
import static org.junit.jupiter.api.Assertions.assertSame;
3836
import static org.junit.jupiter.api.Assertions.assertTrue;
39-
import static org.junit.jupiter.api.Assertions.fail;
4037
import static org.mockito.Mockito.mock;
4138
import static org.mockito.Mockito.when;
4239

@@ -100,19 +97,13 @@ void shouldRespectFailFlag() {
10097
)
10198
);
10299

103-
try {
104-
service.removeGraph(
105-
CatalogRequest.of("some user", "some database"),
106-
GraphName.parse("some graph"),
107-
true
108-
);
109-
110-
fail();
111-
} catch (NoSuchElementException e) {
112-
assertThat(e.getMessage()).isEqualTo(
113-
"Graph with name `some graph` does not exist on database `some database`. It might exist on another database."
114-
);
115-
}
100+
assertThatThrownBy(() -> service.removeGraph(
101+
CatalogRequest.of("some user", "some database"),
102+
GraphName.parse("some graph"),
103+
true
104+
)).isInstanceOf(GraphNotFoundException.class)
105+
.hasMessageContaining(
106+
"Graph with name `some graph` does not exist on database `some database`. It might exist on another database.");
116107
}
117108

118109
}

core/src/test/java/org/neo4j/gds/core/loading/GraphStoreCatalogTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.neo4j.gds.extension.Inject;
3232
import org.neo4j.gds.gdl.GdlFactory;
3333

34-
import java.util.NoSuchElementException;
3534
import java.util.Optional;
3635

3736
import static org.assertj.core.api.Assertions.assertThat;
@@ -429,15 +428,15 @@ void shouldThrowOnMissingGraph() {
429428
var dummyDatabaseId = DatabaseId.of("mydatabase");
430429

431430
// test the get code path
432-
assertThatExceptionOfType(NoSuchElementException.class)
431+
assertThatExceptionOfType(GraphNotFoundException.class)
433432
.isThrownBy(() -> GraphStoreCatalog.get(
434433
CatalogRequest.of(USER_NAME, dummyDatabaseId),
435434
"myGraph"
436435
))
437436
.withMessage("Graph with name `myGraph` does not exist on database `mydatabase`. It might exist on another database.");
438437

439438
// test the drop code path
440-
assertThatExceptionOfType(NoSuchElementException.class)
439+
assertThatExceptionOfType(GraphNotFoundException.class)
441440
.isThrownBy(() -> GraphStoreCatalog.remove(
442441
CatalogRequest.of(USER_NAME, dummyDatabaseId),
443442
"myGraph",

0 commit comments

Comments
 (0)