Skip to content

Commit

Permalink
chore(mis-server): add detailed error information (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
WZD09 authored Sep 29, 2022
1 parent 420628b commit bbe1525
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 43 deletions.
26 changes: 19 additions & 7 deletions apps/mis-server/src/services/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export const accountServiceServer = plugin((server) => {
}, { lockMode: LockMode.PESSIMISTIC_WRITE });

if (!account) {
throw <ServiceError>{ code: Status.NOT_FOUND };
throw <ServiceError>{
code: Status.NOT_FOUND, message: `Account ${accountName} is not found`,
};
}

if (account.blocked) {
Expand All @@ -46,7 +48,9 @@ export const accountServiceServer = plugin((server) => {
}, { lockMode: LockMode.PESSIMISTIC_WRITE });

if (!account) {
throw <ServiceError>{ code: Status.NOT_FOUND };
throw <ServiceError>{
code: Status.NOT_FOUND, message: `Account ${accountName} is not found`,
};
}

if (!account.blocked) {
Expand Down Expand Up @@ -100,12 +104,16 @@ export const accountServiceServer = plugin((server) => {
const user = await em.findOne(User, { userId: ownerId });

if (!user) {
throw <ServiceError> { code: Status.NOT_FOUND };
throw <ServiceError> {
code: Status.NOT_FOUND, message: `User ${ownerId} is not found`,
};
}

const tenant = await em.findOne(Tenant, { name: tenantName });
if (!tenant) {
throw <ServiceError> { code: Status.NOT_FOUND };
throw <ServiceError> {
code: Status.NOT_FOUND, message: `Tenant ${tenantName} is not found`,
};
}

// insert the account now to avoid future conflict
Expand All @@ -120,7 +128,7 @@ export const accountServiceServer = plugin((server) => {
} catch (e) {
if (e instanceof UniqueConstraintViolationException) {
throw <ServiceError>{
code: Status.ALREADY_EXISTS,
code: Status.ALREADY_EXISTS, message: `Account ${accountName} already exists.`,
};
}
}
Expand Down Expand Up @@ -184,7 +192,9 @@ export const accountServiceServer = plugin((server) => {
const account = await em.findOne(Account, { accountName, tenant: { name: tenantName } });

if (!account) {
throw <ServiceError>{ code: Status.NOT_FOUND };
throw <ServiceError>{
code: Status.NOT_FOUND, message: `Account ${accountName} is not found`,
};
}

if (account.whitelist) {
Expand Down Expand Up @@ -217,7 +227,9 @@ export const accountServiceServer = plugin((server) => {
const account = await em.findOne(Account, { accountName, tenant: { name: tenantName } });

if (!account) {
throw <ServiceError>{ code: Status.NOT_FOUND };
throw <ServiceError>{
code: Status.NOT_FOUND, message: `Account ${accountName} is not found`,
};
}

if (!account.whitelist) {
Expand Down
12 changes: 8 additions & 4 deletions apps/mis-server/src/services/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const adminServiceServer = plugin((server) => {

if (!quota) {
throw <ServiceError>{
code: Status.NOT_FOUND,
code: Status.NOT_FOUND, message: `User ${userId} or cluster ${cluster} is not found`,
};
}

Expand All @@ -28,11 +28,15 @@ export const adminServiceServer = plugin((server) => {
);

if (reply.code === "NOT_FOUND") {
throw <ServiceError> { code: Status.NOT_FOUND };
throw <ServiceError> {
code: Status.NOT_FOUND, message: `User ${userId} or cluster ${cluster} is not found`,
};
}

if (reply.code === "INVALID_VALUE") {
throw <ServiceError> { code: Status.INVALID_ARGUMENT };
throw <ServiceError> {
code: Status.INVALID_ARGUMENT, message: `The changed storage quota value ${value} is not valid`,
};
}

quota.storageQuota = reply.currentQuota;
Expand All @@ -52,7 +56,7 @@ export const adminServiceServer = plugin((server) => {

if (!quota) {
throw <ServiceError>{
code: Status.NOT_FOUND,
code: Status.NOT_FOUND, message: `User ${userId} or cluster ${cluster} is not found`,
};
}

Expand Down
33 changes: 30 additions & 3 deletions apps/mis-server/src/services/charging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <ServiceError>{ code: status.NOT_FOUND }; }
if (!entity) {
if (accountName === undefined) {
throw <ServiceError>{
code: status.NOT_FOUND, message: `Tenant ${tenantName} is not found`,
};
} else {
throw <ServiceError>{
code: status.NOT_FOUND, message: `Tenant ${tenantName} or account ${accountName} is not found`,
};
}
}

return [{ balance: decimalToMoney(entity.balance) }];
},
Expand All @@ -43,7 +53,16 @@ export const chargingServiceServer = plugin((server) => {
});

if (!target) {
throw <ServiceError> { code: status.NOT_FOUND };
if (accountName === undefined) {
throw <ServiceError>{
code: status.NOT_FOUND, message: `Tenant ${tenantName} is not found`,
};
} else {
throw <ServiceError>{
code: status.NOT_FOUND, message: `Account ${accountName} or tenant ${tenantName} is not found`,
};
}

}

return await pay({
Expand Down Expand Up @@ -78,7 +97,15 @@ export const chargingServiceServer = plugin((server) => {
});

if (!target) {
throw <ServiceError> { code: status.NOT_FOUND };
if (accountName === undefined) {
throw <ServiceError>{
code: status.NOT_FOUND, message: `Tenant ${tenantName} is not found`,
};
} else {
throw <ServiceError>{
code: status.NOT_FOUND, message: `Account ${accountName} or tenant ${tenantName} is not found`,
};
}
}

return await charge({
Expand Down
4 changes: 3 additions & 1 deletion apps/mis-server/src/services/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ export const initServiceServer = plugin((server) => {
await em.persistAndFlush(initializationTime);
} catch (e) {
if (e instanceof UniqueConstraintViolationException) {
throw <ServiceError> { code: status.ALREADY_EXISTS };
throw <ServiceError> {
code: status.ALREADY_EXISTS, message: "already initialized",
};
} else {
throw e;
}
Expand Down
10 changes: 5 additions & 5 deletions apps/mis-server/src/services/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export const jobServiceServer = plugin((server) => {

if (!job) {
throw <ServiceError>{
code: Status.NOT_FOUND,
code: Status.NOT_FOUND, message: `Job ${biJobIndex} is not found`,
};
}

Expand Down Expand Up @@ -256,10 +256,9 @@ export const jobServiceServer = plugin((server) => {
request: { delta, jobId }, logger,
}),
);

if (reply.code === "NOT_FOUND") {
throw <ServiceError>{
code: Status.NOT_FOUND,
code: Status.NOT_FOUND, message: `Cluster ${cluster} or job ${jobId} is not found.`,
};
}

Expand All @@ -281,7 +280,7 @@ export const jobServiceServer = plugin((server) => {

if (reply.code === "NOT_FOUND") {
throw <ServiceError>{
code: Status.NOT_FOUND,
code: Status.NOT_FOUND, message: `Cluster ${cluster} or job ${jobId} is not found.`,
};
}

Expand Down Expand Up @@ -347,7 +346,8 @@ export const jobServiceServer = plugin((server) => {
if (!(Object.values(AmountStrategy) as string[]).includes(amountStrategy)) {
throw <ServiceError>{
code: status.INVALID_ARGUMENT,
message: `Amount strategy ${amountStrategy} is not valid.` };
message: `Amount strategy ${amountStrategy} is not valid.`,
};
}

const item = new JobPriceItem({
Expand Down
6 changes: 3 additions & 3 deletions apps/mis-server/src/services/jobChargeLimit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ export const jobChargeLimitServer = plugin((server) => {
if (!userAccount) {
throw <ServiceError>{
code: Status.NOT_FOUND,
details: "User is not found in account.",
details: `User ${userId} is not found in account`,
};
}

if (!userAccount.jobChargeLimit) {
throw <ServiceError> {
code: Status.NOT_FOUND,
details: "The user in account has no limit",
details: `The user ${userId} in account ${accountName} has no limit`,
};
}

Expand Down Expand Up @@ -61,7 +61,7 @@ export const jobChargeLimitServer = plugin((server) => {
if (!userAccount) {
throw <ServiceError>{
code: Status.NOT_FOUND,
details: "User is not found in account.",
details: `User ${userId} is not found in account.`,
};
}

Expand Down
Loading

0 comments on commit bbe1525

Please sign in to comment.