Skip to content

Commit aa52b3e

Browse files
kylos101roboquat
authored andcommitted
[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 efd72d8 commit aa52b3e

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

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

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,24 @@ 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 (!!clusterByUrl) {
86+
if (clusterByUrl.length > 0) {
87+
throw new GRPCError(grpc.status.ALREADY_EXISTS, `a WorkspaceCluster with url ${req.url} already exists in the DB`);
88+
}
89+
}
90+
8891
// store the ws-manager into the database
8992
let perfereability = Preferability.NONE;
9093
let govern = false;
@@ -154,7 +157,7 @@ export class ClusterService implements IClusterServiceServer {
154157
const req = call.request.toObject();
155158
const cluster = await this.clusterDB.findByName(req.name);
156159
if (!cluster) {
157-
throw new GRPCError(grpc.status.ALREADY_EXISTS, `a WorkspaceCluster with name ${req.name} already exists in the DB!`);
160+
throw new GRPCError(grpc.status.NOT_FOUND, `a WorkspaceCluster with name ${req.name} does not exist in the DB!`);
158161
}
159162

160163
if (call.request.hasMaxScore()) {
@@ -258,8 +261,6 @@ export class ClusterService implements IClusterServiceServer {
258261
response.addStatus(clusterStatus);
259262
}
260263

261-
log.info("response status for clusters.list", response.getStatusList())
262-
263264
callback(null, response);
264265
} catch (err) {
265266
callback(mapToGRPCError(err), null);

0 commit comments

Comments
 (0)