Skip to content

Updated Error Response on API End-point #524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/server/routes/backend-wallet/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { WalletType } from "../../../schema/wallet";
import { getConfig } from "../../../utils/cache/getConfig";
import { createCustomError } from "../../middleware/error";
import { standardResponseSchema } from "../../schemas/sharedApiSchemas";
import { importAwsKmsWallet } from "../../utils/wallets/importAwsKmsWallet";
import { importGcpKmsWallet } from "../../utils/wallets/importGcpKmsWallet";
Expand Down Expand Up @@ -135,16 +136,20 @@ export const importBackendWallet = async (fastify: FastifyInstance) => {
password,
});
} else {
throw new Error(
throw createCustomError(
`Please provide either 'privateKey', 'mnemonic', or 'encryptedJson' & 'password' to import a wallet.`,
StatusCodes.BAD_REQUEST,
"MISSING_PARAMETERS",
);
}
break;
case WalletType.awsKms:
const { awsKmsArn, awsKmsKeyId } = request.body as any;
if (!(awsKmsArn && awsKmsKeyId)) {
throw new Error(
throw createCustomError(
`Please provide 'awsKmsArn' and 'awsKmsKeyId' to import a wallet.`,
StatusCodes.BAD_REQUEST,
"MISSING_PARAMETERS",
);
}

Expand All @@ -156,8 +161,10 @@ export const importBackendWallet = async (fastify: FastifyInstance) => {
case WalletType.gcpKms:
const { gcpKmsKeyId, gcpKmsKeyVersionId } = request.body as any;
if (!(gcpKmsKeyId && gcpKmsKeyVersionId)) {
throw new Error(
throw createCustomError(
`Please provide 'gcpKmsKeyId' and 'gcpKmsKeyVersionId' to import a wallet.`,
StatusCodes.BAD_REQUEST,
"MISSING_PARAMETERS",
);
}

Expand Down
14 changes: 12 additions & 2 deletions src/server/routes/backend-wallet/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { queueTx } from "../../../db/transactions/queueTx";
import { queueTxRaw } from "../../../db/transactions/queueTxRaw";
import { getContract } from "../../../utils/cache/getContract";
import { getSdk } from "../../../utils/cache/getSdk";
import { createCustomError } from "../../middleware/error";
import {
requestQuerystringSchema,
standardResponseSchema,
Expand Down Expand Up @@ -80,12 +81,21 @@ export async function transfer(fastify: FastifyInstance) {
let queueId: string | null = null;
if (isNativeToken(currencyAddress)) {
const walletAddress = await sdk.getSigner()?.getAddress();
if (!walletAddress) throw new Error("No wallet address");
if (!walletAddress)
throw createCustomError(
"No wallet address",
StatusCodes.BAD_REQUEST,
"NO_WALLET_ADDRESS",
);

const balance = await sdk.getBalance(walletAddress);

if (balance.value.lt(normalizedValue)) {
throw new Error("Insufficient balance");
throw createCustomError(
"Insufficient balance",
StatusCodes.BAD_GATEWAY,
"INSUFFICIENT_BALANCE",
);
}

const params = {
Expand Down
5 changes: 4 additions & 1 deletion src/server/routes/configuration/cors/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { updateConfiguration } from "../../../../db/configuration/updateConfiguration";
import { getConfig } from "../../../../utils/cache/getConfig";
import { createCustomError } from "../../../middleware/error";
import { standardResponseSchema } from "../../../schemas/sharedApiSchemas";
import { mandatoryAllowedCorsUrls } from "../../../utils/cors-urls";
import { ReplySchema } from "./get";
Expand Down Expand Up @@ -49,8 +50,10 @@ export async function removeUrlToCorsConfiguration(fastify: FastifyInstance) {
mandatoryAllowedCorsUrls.includes(url),
);
if (containsMandatoryUrl) {
throw new Error(
throw createCustomError(
`Cannot remove URLs: ${mandatoryAllowedCorsUrls.join(",")}`,
StatusCodes.BAD_REQUEST,
"BAD_REQUEST",
);
}

Expand Down
13 changes: 11 additions & 2 deletions src/server/routes/configuration/wallets/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { StatusCodes } from "http-status-codes";
import { updateConfiguration } from "../../../../db/configuration/updateConfiguration";
import { WalletType } from "../../../../schema/wallet";
import { getConfig } from "../../../../utils/cache/getConfig";
import { createCustomError } from "../../../middleware/error";
import { standardResponseSchema } from "../../../schemas/sharedApiSchemas";
import { ReplySchema } from "./get";

Expand Down Expand Up @@ -86,7 +87,11 @@ export async function updateWalletsConfiguration(fastify: FastifyInstance) {
!req.body.awsSecretAccessKey ||
!req.body.awsRegion
) {
throw new Error("Please specify all AWS KMS configuration.");
throw createCustomError(
"Please specify all AWS KMS configuration.",
StatusCodes.BAD_REQUEST,
"BAD_REQUEST",
);
}

await updateConfiguration({
Expand All @@ -108,7 +113,11 @@ export async function updateWalletsConfiguration(fastify: FastifyInstance) {
!req.body.gcpApplicationCredentialEmail ||
!req.body.gcpApplicationCredentialPrivateKey
) {
throw new Error("Please specify all GCP KMS configuration.");
throw createCustomError(
"Please specify all GCP KMS configuration.",
StatusCodes.BAD_REQUEST,
"BAD_REQUEST",
);
}

await updateConfiguration({
Expand Down
14 changes: 10 additions & 4 deletions src/worker/tasks/processEventLogsWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ const handler: Processor<any, void, string> = async (job: Job<string>) => {
// Store logs to DB.
const insertedLogs = await bulkInsertContractEventLogs({ logs });

if (insertedLogs.length === 0) {
return;
}

// Enqueue webhooks.
// This step should happen immediately after inserting to DB.
for (const eventLog of insertedLogs) {
Expand All @@ -54,13 +58,15 @@ const handler: Processor<any, void, string> = async (job: Job<string>) => {
}

// Any logs inserted in a delayed job indicates missed logs in the realtime job.
if (job.delay > 0) {
if (job.opts.delay && job.opts.delay > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the first condition? Is opts.delay sometimes undefined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, it can be undefined

logger({
service: "worker",
level: "warn",
message: `Found ${insertedLogs.length} logs on ${chainId} after ${
job.delay / 1000
}s.`,
message: `Found ${
insertedLogs.length
} logs on chain: ${chainId}, block: ${insertedLogs.map(
(log) => log.blockNumber,
)} after ${job.opts.delay / 1000}s.`,
});
}
};
Expand Down
14 changes: 10 additions & 4 deletions src/worker/tasks/processTransactionReceiptsWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ const handler: Processor<any, void, string> = async (job: Job<string>) => {
receipts,
});

if (insertedReceipts.length === 0) {
return;
}

// Enqueue webhooks.
// This step should happen immediately after inserting to DB.
for (const transactionReceipt of insertedReceipts) {
Expand All @@ -142,13 +146,15 @@ const handler: Processor<any, void, string> = async (job: Job<string>) => {
}

// Any receipts inserted in a delayed job indicates missed receipts in the realtime job.
if (job.delay > 0) {
if (job.opts.delay && job.opts.delay > 0) {
logger({
service: "worker",
level: "warn",
message: `Found ${insertedReceipts.length} receipts on ${chainId} after ${
job.delay / 1000
}s.`,
message: `Found ${
insertedReceipts.length
} receipts on chain: ${chainId}, block: ${insertedReceipts.map(
(receipt) => receipt.blockNumber,
)} after ${job.delay / 1000}s.`,
});
}
};
Expand Down
Loading