Skip to content

Commit d9eb681

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 d9eb681

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

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

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,19 @@ 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 [clusterByName, clusterByUrl] = await Promise.all([
75+
this.clusterDB.findByName(req.name),
76+
this.clusterDB.findFiltered({ url: req.url })
8677
]);
8778

79+
if (!!clusterByName) {
80+
throw new GRPCError(grpc.status.ALREADY_EXISTS, `a WorkspaceCluster with name ${req.name} already exists in the DB`);
81+
}
82+
if (Array.isArray(clusterByUrl) && clusterByUrl.length > 0) {
83+
throw new GRPCError(grpc.status.ALREADY_EXISTS, `a WorkspaceCluster with url ${req.url} already exists in the DB`);
84+
}
85+
8886
// store the ws-manager into the database
8987
let perfereability = Preferability.NONE;
9088
let govern = false;
@@ -154,7 +152,7 @@ export class ClusterService implements IClusterServiceServer {
154152
const req = call.request.toObject();
155153
const cluster = await this.clusterDB.findByName(req.name);
156154
if (!cluster) {
157-
throw new GRPCError(grpc.status.ALREADY_EXISTS, `a WorkspaceCluster with name ${req.name} already exists in the DB!`);
155+
throw new GRPCError(grpc.status.NOT_FOUND, `a WorkspaceCluster with name ${req.name} does not exist in the DB!`);
158156
}
159157

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

0 commit comments

Comments
 (0)