Skip to content

Commit 1eb80c1

Browse files
authored
Updated Error Response on API End-point (#524)
* Updated Error Response on API End-point * updated check + log to indicate missed events/receipts
1 parent 666f1e1 commit 1eb80c1

File tree

6 files changed

+57
-16
lines changed

6 files changed

+57
-16
lines changed

src/server/routes/backend-wallet/import.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { FastifyInstance } from "fastify";
33
import { StatusCodes } from "http-status-codes";
44
import { WalletType } from "../../../schema/wallet";
55
import { getConfig } from "../../../utils/cache/getConfig";
6+
import { createCustomError } from "../../middleware/error";
67
import { standardResponseSchema } from "../../schemas/sharedApiSchemas";
78
import { importAwsKmsWallet } from "../../utils/wallets/importAwsKmsWallet";
89
import { importGcpKmsWallet } from "../../utils/wallets/importGcpKmsWallet";
@@ -135,16 +136,20 @@ export const importBackendWallet = async (fastify: FastifyInstance) => {
135136
password,
136137
});
137138
} else {
138-
throw new Error(
139+
throw createCustomError(
139140
`Please provide either 'privateKey', 'mnemonic', or 'encryptedJson' & 'password' to import a wallet.`,
141+
StatusCodes.BAD_REQUEST,
142+
"MISSING_PARAMETERS",
140143
);
141144
}
142145
break;
143146
case WalletType.awsKms:
144147
const { awsKmsArn, awsKmsKeyId } = request.body as any;
145148
if (!(awsKmsArn && awsKmsKeyId)) {
146-
throw new Error(
149+
throw createCustomError(
147150
`Please provide 'awsKmsArn' and 'awsKmsKeyId' to import a wallet.`,
151+
StatusCodes.BAD_REQUEST,
152+
"MISSING_PARAMETERS",
148153
);
149154
}
150155

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

src/server/routes/backend-wallet/transfer.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { queueTx } from "../../../db/transactions/queueTx";
1111
import { queueTxRaw } from "../../../db/transactions/queueTxRaw";
1212
import { getContract } from "../../../utils/cache/getContract";
1313
import { getSdk } from "../../../utils/cache/getSdk";
14+
import { createCustomError } from "../../middleware/error";
1415
import {
1516
requestQuerystringSchema,
1617
standardResponseSchema,
@@ -80,12 +81,21 @@ export async function transfer(fastify: FastifyInstance) {
8081
let queueId: string | null = null;
8182
if (isNativeToken(currencyAddress)) {
8283
const walletAddress = await sdk.getSigner()?.getAddress();
83-
if (!walletAddress) throw new Error("No wallet address");
84+
if (!walletAddress)
85+
throw createCustomError(
86+
"No wallet address",
87+
StatusCodes.BAD_REQUEST,
88+
"NO_WALLET_ADDRESS",
89+
);
8490

8591
const balance = await sdk.getBalance(walletAddress);
8692

8793
if (balance.value.lt(normalizedValue)) {
88-
throw new Error("Insufficient balance");
94+
throw createCustomError(
95+
"Insufficient balance",
96+
StatusCodes.BAD_GATEWAY,
97+
"INSUFFICIENT_BALANCE",
98+
);
8999
}
90100

91101
const params = {

src/server/routes/configuration/cors/remove.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { FastifyInstance } from "fastify";
33
import { StatusCodes } from "http-status-codes";
44
import { updateConfiguration } from "../../../../db/configuration/updateConfiguration";
55
import { getConfig } from "../../../../utils/cache/getConfig";
6+
import { createCustomError } from "../../../middleware/error";
67
import { standardResponseSchema } from "../../../schemas/sharedApiSchemas";
78
import { mandatoryAllowedCorsUrls } from "../../../utils/cors-urls";
89
import { ReplySchema } from "./get";
@@ -49,8 +50,10 @@ export async function removeUrlToCorsConfiguration(fastify: FastifyInstance) {
4950
mandatoryAllowedCorsUrls.includes(url),
5051
);
5152
if (containsMandatoryUrl) {
52-
throw new Error(
53+
throw createCustomError(
5354
`Cannot remove URLs: ${mandatoryAllowedCorsUrls.join(",")}`,
55+
StatusCodes.BAD_REQUEST,
56+
"BAD_REQUEST",
5457
);
5558
}
5659

src/server/routes/configuration/wallets/update.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { StatusCodes } from "http-status-codes";
44
import { updateConfiguration } from "../../../../db/configuration/updateConfiguration";
55
import { WalletType } from "../../../../schema/wallet";
66
import { getConfig } from "../../../../utils/cache/getConfig";
7+
import { createCustomError } from "../../../middleware/error";
78
import { standardResponseSchema } from "../../../schemas/sharedApiSchemas";
89
import { ReplySchema } from "./get";
910

@@ -86,7 +87,11 @@ export async function updateWalletsConfiguration(fastify: FastifyInstance) {
8687
!req.body.awsSecretAccessKey ||
8788
!req.body.awsRegion
8889
) {
89-
throw new Error("Please specify all AWS KMS configuration.");
90+
throw createCustomError(
91+
"Please specify all AWS KMS configuration.",
92+
StatusCodes.BAD_REQUEST,
93+
"BAD_REQUEST",
94+
);
9095
}
9196

9297
await updateConfiguration({
@@ -108,7 +113,11 @@ export async function updateWalletsConfiguration(fastify: FastifyInstance) {
108113
!req.body.gcpApplicationCredentialEmail ||
109114
!req.body.gcpApplicationCredentialPrivateKey
110115
) {
111-
throw new Error("Please specify all GCP KMS configuration.");
116+
throw createCustomError(
117+
"Please specify all GCP KMS configuration.",
118+
StatusCodes.BAD_REQUEST,
119+
"BAD_REQUEST",
120+
);
112121
}
113122

114123
await updateConfiguration({

src/worker/tasks/processEventLogsWorker.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ const handler: Processor<any, void, string> = async (job: Job<string>) => {
4040
// Store logs to DB.
4141
const insertedLogs = await bulkInsertContractEventLogs({ logs });
4242

43+
if (insertedLogs.length === 0) {
44+
return;
45+
}
46+
4347
// Enqueue webhooks.
4448
// This step should happen immediately after inserting to DB.
4549
for (const eventLog of insertedLogs) {
@@ -54,13 +58,15 @@ const handler: Processor<any, void, string> = async (job: Job<string>) => {
5458
}
5559

5660
// Any logs inserted in a delayed job indicates missed logs in the realtime job.
57-
if (job.delay > 0) {
61+
if (job.opts.delay && job.opts.delay > 0) {
5862
logger({
5963
service: "worker",
6064
level: "warn",
61-
message: `Found ${insertedLogs.length} logs on ${chainId} after ${
62-
job.delay / 1000
63-
}s.`,
65+
message: `Found ${
66+
insertedLogs.length
67+
} logs on chain: ${chainId}, block: ${insertedLogs.map(
68+
(log) => log.blockNumber,
69+
)} after ${job.opts.delay / 1000}s.`,
6470
});
6571
}
6672
};

src/worker/tasks/processTransactionReceiptsWorker.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ const handler: Processor<any, void, string> = async (job: Job<string>) => {
127127
receipts,
128128
});
129129

130+
if (insertedReceipts.length === 0) {
131+
return;
132+
}
133+
130134
// Enqueue webhooks.
131135
// This step should happen immediately after inserting to DB.
132136
for (const transactionReceipt of insertedReceipts) {
@@ -142,13 +146,15 @@ const handler: Processor<any, void, string> = async (job: Job<string>) => {
142146
}
143147

144148
// Any receipts inserted in a delayed job indicates missed receipts in the realtime job.
145-
if (job.delay > 0) {
149+
if (job.opts.delay && job.opts.delay > 0) {
146150
logger({
147151
service: "worker",
148152
level: "warn",
149-
message: `Found ${insertedReceipts.length} receipts on ${chainId} after ${
150-
job.delay / 1000
151-
}s.`,
153+
message: `Found ${
154+
insertedReceipts.length
155+
} receipts on chain: ${chainId}, block: ${insertedReceipts.map(
156+
(receipt) => receipt.blockNumber,
157+
)} after ${job.delay / 1000}s.`,
152158
});
153159
}
154160
};

0 commit comments

Comments
 (0)