Skip to content

Commit 03b29fb

Browse files
committed
[ws-manager-bridge] improve error handling for workspace cluster registration and update
Prevent duplicate workspace cluster registration & improve error message for update when workspace cluster doesn't exist
1 parent 4b87fa1 commit 03b29fb

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

components/ws-manager-bridge/src/cluster-service-server.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,22 @@ export class ClusterService implements IClusterServiceServer {
7070
try {
7171
// check if the name or URL are already registered/in use
7272
const req = call.request.toObject();
73-
await Promise.all([
74-
async () => {
75-
const oldCluster = await this.clusterDB.findByName(req.name);
76-
if (!oldCluster) {
77-
throw new GRPCError(grpc.status.ALREADY_EXISTS, `a WorkspaceCluster with name ${req.name} already exists in the DB`);
78-
}
79-
},
80-
async () => {
81-
const oldCluster = await this.clusterDB.findFiltered({ url: req.url });
82-
if (!oldCluster) {
83-
throw new GRPCError(grpc.status.ALREADY_EXISTS, `a WorkspaceCluster with url ${req.url} already exists in the DB`);
84-
}
85-
}
73+
74+
const clusterByNamePromise = this.clusterDB.findByName(req.name);
75+
const clusterByUrlPromise = this.clusterDB.findFiltered({ url: req.url })
76+
77+
const [clusterByName, clusterByUrl] = await Promise.all([
78+
clusterByNamePromise,
79+
clusterByUrlPromise
8680
]);
8781

82+
if (!!clusterByName) {
83+
throw new GRPCError(grpc.status.ALREADY_EXISTS, `a WorkspaceCluster with name ${req.name} already exists in the DB`);
84+
}
85+
if (Array.isArray(clusterByUrl) && clusterByUrl.length > 0) {
86+
throw new GRPCError(grpc.status.ALREADY_EXISTS, `a WorkspaceCluster with url ${req.url} already exists in the DB`);
87+
}
88+
8889
// store the ws-manager into the database
8990
let perfereability = Preferability.NONE;
9091
let govern = false;
@@ -154,7 +155,7 @@ export class ClusterService implements IClusterServiceServer {
154155
const req = call.request.toObject();
155156
const cluster = await this.clusterDB.findByName(req.name);
156157
if (!cluster) {
157-
throw new GRPCError(grpc.status.ALREADY_EXISTS, `a WorkspaceCluster with name ${req.name} already exists in the DB!`);
158+
throw new GRPCError(grpc.status.NOT_FOUND, `a WorkspaceCluster with name ${req.name} does not exist in the DB!`);
158159
}
159160

160161
if (call.request.hasMaxScore()) {

0 commit comments

Comments
 (0)