From bbe1525ec4ace12049aab59bb5b652c4d76e0eae Mon Sep 17 00:00:00 2001 From: WZD09 <102740885+WZD09@users.noreply.github.com> Date: Thu, 29 Sep 2022 23:00:04 +0800 Subject: [PATCH] chore(mis-server): add detailed error information (#198) --- apps/mis-server/src/services/account.ts | 26 +++++++--- apps/mis-server/src/services/admin.ts | 12 +++-- apps/mis-server/src/services/charging.ts | 33 +++++++++++-- apps/mis-server/src/services/init.ts | 4 +- apps/mis-server/src/services/job.ts | 10 ++-- .../mis-server/src/services/jobChargeLimit.ts | 6 +-- apps/mis-server/src/services/user.ts | 47 +++++++++++-------- 7 files changed, 95 insertions(+), 43 deletions(-) diff --git a/apps/mis-server/src/services/account.ts b/apps/mis-server/src/services/account.ts index f81a74ab1a..c11aa6133b 100644 --- a/apps/mis-server/src/services/account.ts +++ b/apps/mis-server/src/services/account.ts @@ -23,7 +23,9 @@ export const accountServiceServer = plugin((server) => { }, { lockMode: LockMode.PESSIMISTIC_WRITE }); if (!account) { - throw { code: Status.NOT_FOUND }; + throw { + code: Status.NOT_FOUND, message: `Account ${accountName} is not found`, + }; } if (account.blocked) { @@ -46,7 +48,9 @@ export const accountServiceServer = plugin((server) => { }, { lockMode: LockMode.PESSIMISTIC_WRITE }); if (!account) { - throw { code: Status.NOT_FOUND }; + throw { + code: Status.NOT_FOUND, message: `Account ${accountName} is not found`, + }; } if (!account.blocked) { @@ -100,12 +104,16 @@ export const accountServiceServer = plugin((server) => { const user = await em.findOne(User, { userId: ownerId }); if (!user) { - throw { code: Status.NOT_FOUND }; + throw { + code: Status.NOT_FOUND, message: `User ${ownerId} is not found`, + }; } const tenant = await em.findOne(Tenant, { name: tenantName }); if (!tenant) { - throw { code: Status.NOT_FOUND }; + throw { + code: Status.NOT_FOUND, message: `Tenant ${tenantName} is not found`, + }; } // insert the account now to avoid future conflict @@ -120,7 +128,7 @@ export const accountServiceServer = plugin((server) => { } catch (e) { if (e instanceof UniqueConstraintViolationException) { throw { - code: Status.ALREADY_EXISTS, + code: Status.ALREADY_EXISTS, message: `Account ${accountName} already exists.`, }; } } @@ -184,7 +192,9 @@ export const accountServiceServer = plugin((server) => { const account = await em.findOne(Account, { accountName, tenant: { name: tenantName } }); if (!account) { - throw { code: Status.NOT_FOUND }; + throw { + code: Status.NOT_FOUND, message: `Account ${accountName} is not found`, + }; } if (account.whitelist) { @@ -217,7 +227,9 @@ export const accountServiceServer = plugin((server) => { const account = await em.findOne(Account, { accountName, tenant: { name: tenantName } }); if (!account) { - throw { code: Status.NOT_FOUND }; + throw { + code: Status.NOT_FOUND, message: `Account ${accountName} is not found`, + }; } if (!account.whitelist) { diff --git a/apps/mis-server/src/services/admin.ts b/apps/mis-server/src/services/admin.ts index c94dec1181..e7c995ec89 100644 --- a/apps/mis-server/src/services/admin.ts +++ b/apps/mis-server/src/services/admin.ts @@ -17,7 +17,7 @@ export const adminServiceServer = plugin((server) => { if (!quota) { throw { - code: Status.NOT_FOUND, + code: Status.NOT_FOUND, message: `User ${userId} or cluster ${cluster} is not found`, }; } @@ -28,11 +28,15 @@ export const adminServiceServer = plugin((server) => { ); if (reply.code === "NOT_FOUND") { - throw { code: Status.NOT_FOUND }; + throw { + code: Status.NOT_FOUND, message: `User ${userId} or cluster ${cluster} is not found`, + }; } if (reply.code === "INVALID_VALUE") { - throw { code: Status.INVALID_ARGUMENT }; + throw { + code: Status.INVALID_ARGUMENT, message: `The changed storage quota value ${value} is not valid`, + }; } quota.storageQuota = reply.currentQuota; @@ -52,7 +56,7 @@ export const adminServiceServer = plugin((server) => { if (!quota) { throw { - code: Status.NOT_FOUND, + code: Status.NOT_FOUND, message: `User ${userId} or cluster ${cluster} is not found`, }; } diff --git a/apps/mis-server/src/services/charging.ts b/apps/mis-server/src/services/charging.ts index 716b46d20a..1ec234d7b1 100644 --- a/apps/mis-server/src/services/charging.ts +++ b/apps/mis-server/src/services/charging.ts @@ -21,7 +21,17 @@ export const chargingServiceServer = plugin((server) => { ? await em.findOne(Tenant, { name: tenantName }) : await em.findOne(Account, { tenant: { name: tenantName }, accountName }); - if (!entity) { throw { code: status.NOT_FOUND }; } + if (!entity) { + if (accountName === undefined) { + throw { + code: status.NOT_FOUND, message: `Tenant ${tenantName} is not found`, + }; + } else { + throw { + code: status.NOT_FOUND, message: `Tenant ${tenantName} or account ${accountName} is not found`, + }; + } + } return [{ balance: decimalToMoney(entity.balance) }]; }, @@ -43,7 +53,16 @@ export const chargingServiceServer = plugin((server) => { }); if (!target) { - throw { code: status.NOT_FOUND }; + if (accountName === undefined) { + throw { + code: status.NOT_FOUND, message: `Tenant ${tenantName} is not found`, + }; + } else { + throw { + code: status.NOT_FOUND, message: `Account ${accountName} or tenant ${tenantName} is not found`, + }; + } + } return await pay({ @@ -78,7 +97,15 @@ export const chargingServiceServer = plugin((server) => { }); if (!target) { - throw { code: status.NOT_FOUND }; + if (accountName === undefined) { + throw { + code: status.NOT_FOUND, message: `Tenant ${tenantName} is not found`, + }; + } else { + throw { + code: status.NOT_FOUND, message: `Account ${accountName} or tenant ${tenantName} is not found`, + }; + } } return await charge({ diff --git a/apps/mis-server/src/services/init.ts b/apps/mis-server/src/services/init.ts index fe9432c3a9..7ab11ff9c7 100644 --- a/apps/mis-server/src/services/init.ts +++ b/apps/mis-server/src/services/init.ts @@ -88,7 +88,9 @@ export const initServiceServer = plugin((server) => { await em.persistAndFlush(initializationTime); } catch (e) { if (e instanceof UniqueConstraintViolationException) { - throw { code: status.ALREADY_EXISTS }; + throw { + code: status.ALREADY_EXISTS, message: "already initialized", + }; } else { throw e; } diff --git a/apps/mis-server/src/services/job.ts b/apps/mis-server/src/services/job.ts index 1e97004552..6375f1db9d 100644 --- a/apps/mis-server/src/services/job.ts +++ b/apps/mis-server/src/services/job.ts @@ -216,7 +216,7 @@ export const jobServiceServer = plugin((server) => { if (!job) { throw { - code: Status.NOT_FOUND, + code: Status.NOT_FOUND, message: `Job ${biJobIndex} is not found`, }; } @@ -256,10 +256,9 @@ export const jobServiceServer = plugin((server) => { request: { delta, jobId }, logger, }), ); - if (reply.code === "NOT_FOUND") { throw { - code: Status.NOT_FOUND, + code: Status.NOT_FOUND, message: `Cluster ${cluster} or job ${jobId} is not found.`, }; } @@ -281,7 +280,7 @@ export const jobServiceServer = plugin((server) => { if (reply.code === "NOT_FOUND") { throw { - code: Status.NOT_FOUND, + code: Status.NOT_FOUND, message: `Cluster ${cluster} or job ${jobId} is not found.`, }; } @@ -347,7 +346,8 @@ export const jobServiceServer = plugin((server) => { if (!(Object.values(AmountStrategy) as string[]).includes(amountStrategy)) { throw { code: status.INVALID_ARGUMENT, - message: `Amount strategy ${amountStrategy} is not valid.` }; + message: `Amount strategy ${amountStrategy} is not valid.`, + }; } const item = new JobPriceItem({ diff --git a/apps/mis-server/src/services/jobChargeLimit.ts b/apps/mis-server/src/services/jobChargeLimit.ts index 444b0415db..91cf8a77f3 100644 --- a/apps/mis-server/src/services/jobChargeLimit.ts +++ b/apps/mis-server/src/services/jobChargeLimit.ts @@ -21,14 +21,14 @@ export const jobChargeLimitServer = plugin((server) => { if (!userAccount) { throw { code: Status.NOT_FOUND, - details: "User is not found in account.", + details: `User ${userId} is not found in account`, }; } if (!userAccount.jobChargeLimit) { throw { code: Status.NOT_FOUND, - details: "The user in account has no limit", + details: `The user ${userId} in account ${accountName} has no limit`, }; } @@ -61,7 +61,7 @@ export const jobChargeLimitServer = plugin((server) => { if (!userAccount) { throw { code: Status.NOT_FOUND, - details: "User is not found in account.", + details: `User ${userId} is not found in account.`, }; } diff --git a/apps/mis-server/src/services/user.ts b/apps/mis-server/src/services/user.ts index 050af06036..df52c4d59b 100644 --- a/apps/mis-server/src/services/user.ts +++ b/apps/mis-server/src/services/user.ts @@ -69,7 +69,7 @@ export const userServiceServer = plugin((server) => { if (!user) { throw { - code: Status.NOT_FOUND, + code: Status.NOT_FOUND, message: `User ${userId}, tenant ${tenantName} is not found`, }; } @@ -105,7 +105,7 @@ export const userServiceServer = plugin((server) => { if (reply.code === "NOT_FOUND") { throw { - code: Status.NOT_FOUND, + code: Status.NOT_FOUND, message: `User ${userId} is not found.`, }; } @@ -128,12 +128,13 @@ export const userServiceServer = plugin((server) => { if (!account || !user) { throw { code: Status.NOT_FOUND, + message: `Account ${accountName} or user ${userId}, tenant ${tenantName} is not found.`, }; } if (account.users.getItems().some((x) => x.user.getEntity().userId === userId)) { throw { - code: Status.ALREADY_EXISTS, + code: Status.ALREADY_EXISTS, message: `User ${userId} already in the account ${accountName}.`, }; } @@ -165,13 +166,14 @@ export const userServiceServer = plugin((server) => { if (!userAccount) { throw { - code: Status.NOT_FOUND, + code: Status.NOT_FOUND, message:`User ${userId} or account ${accountName} is not found.`, }; } if (userAccount.role === UserRole.OWNER) { throw { code: Status.OUT_OF_RANGE, + message: `User ${userId} is the owner of the account ${accountName}。`, }; } @@ -195,13 +197,13 @@ export const userServiceServer = plugin((server) => { if (!user) { throw { - code: Status.NOT_FOUND, + code: Status.NOT_FOUND, message: `User ${userId} or account ${accountName} is not found.`, }; } if (user.status === UserStatus.BLOCKED) { throw { - code: Status.FAILED_PRECONDITION, + code: Status.FAILED_PRECONDITION, message: `User ${userId} is already blocked.`, }; } @@ -224,13 +226,13 @@ export const userServiceServer = plugin((server) => { if (!user) { throw { - code: Status.NOT_FOUND, + code: Status.NOT_FOUND, message:`User ${userId} or account ${accountName} is not found.`, }; } if (user.status === UserStatus.UNBLOCKED) { throw { - code: Status.FAILED_PRECONDITION, + code: Status.FAILED_PRECONDITION, message: `User ${userId} is already unblocked.`, }; } @@ -253,13 +255,13 @@ export const userServiceServer = plugin((server) => { if (!user) { throw { - code: Status.NOT_FOUND, + code: Status.NOT_FOUND, message:`User ${userId} or account ${accountName} is not found.`, }; } if (user.role === UserRole.ADMIN) { throw { - code: Status.FAILED_PRECONDITION, + code: Status.FAILED_PRECONDITION, message: `User ${userId} is already admin.`, }; } @@ -279,13 +281,13 @@ export const userServiceServer = plugin((server) => { if (!user) { throw { - code: Status.NOT_FOUND, + code: Status.NOT_FOUND, message:`User ${userId} or account ${accountName} is not found.`, }; } if (user.role === UserRole.USER) { throw { - code: Status.FAILED_PRECONDITION, + code: Status.FAILED_PRECONDITION, message: `User ${userId} is already not admin.`, }; } @@ -302,7 +304,7 @@ export const userServiceServer = plugin((server) => { if (!tenant) { throw { code: Status.NOT_FOUND, details: "Tenant is not found." }; } - + // creat user in database const user = new User({ name, userId: identityId, tenant, email }); user.storageQuotas.add(...Object.keys(clusters).map((x) => new StorageQuota({ @@ -315,13 +317,14 @@ export const userServiceServer = plugin((server) => { await em.persistAndFlush(user); } catch (e) { if (e instanceof UniqueConstraintViolationException) { - throw { code: Status.ALREADY_EXISTS }; + throw { code: Status.ALREADY_EXISTS, message:`User with id ${identityId} already exists.` }; } else { throw e; } } // call auth + // 调用认证系统的创建用户接口,在底层认证系统中创建用户 const rep = await fetch(misConfig.authUrl + "/user", { method: "POST", body: JSON.stringify({ @@ -338,16 +341,20 @@ export const userServiceServer = plugin((server) => { logger.info("Calling auth completed. %o", rep); + // 如果调用认证系统的创建用户接口失败,删除第一步在数据库中创建的用户 + if (!rep.ok) { await em.removeAndFlush(user); if (rep.status === 409) { - throw { code: Status.ALREADY_EXISTS }; + throw { + code: Status.ALREADY_EXISTS, message:`User with id ${user.id} already exists.`, + }; } logger.info("Error creating user in auth. code: %d, body: %o", rep.status, await rep.text()); - throw { code: Status.INTERNAL, message: "Error creating user in auth" }; + throw { code: Status.INTERNAL, message: `Error creating user ${user.id} in auth.` }; } return [{ id: user.id }]; @@ -358,7 +365,7 @@ export const userServiceServer = plugin((server) => { const user = await em.findOne(User, { userId, tenant: { name: tenantName } }); if (!user) { - throw { code: Status.NOT_FOUND }; + throw { code: Status.NOT_FOUND, message:`User ${userId} is not found.` }; } // find if the user is an owner of any account @@ -370,7 +377,7 @@ export const userServiceServer = plugin((server) => { if (accountUser) { throw { code: Status.FAILED_PRECONDITION, - details: "User is an owner of an account.", + details: `User ${userId} is an owner of an account.`, }; } @@ -384,7 +391,7 @@ export const userServiceServer = plugin((server) => { const user = await em.findOne(User, { userId, tenant: { name: tenantName } }, { fields: ["name"]}); if (!user) { - throw { code: Status.NOT_FOUND }; + throw { code: Status.NOT_FOUND, message:`User ${userId} is not found.` }; } return [{ name: user.name }]; @@ -421,7 +428,7 @@ export const userServiceServer = plugin((server) => { }, { populate: ["accounts", "accounts.account", "tenant"]}); if (!user) { - throw { code: Status.NOT_FOUND }; + throw { code: Status.NOT_FOUND, message:`User ${userId} is not found.` }; } return [{