Skip to content

Commit

Permalink
Fix: Worker Error Reporting (#480)
Browse files Browse the repository at this point in the history
* throwing Error for Worker onError hook to log right message

* more errors

---------

Co-authored-by: Phillip Ho <[email protected]>
  • Loading branch information
farhanW3 and arcoraven authored Apr 6, 2024
1 parent b90de6b commit c16b97b
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 23 deletions.
3 changes: 1 addition & 2 deletions src/server/routes/chain/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,11 @@ export async function getChainData(fastify: FastifyInstance) {
}

if (!chainData) {
const error = createCustomError(
throw createCustomError(
"Chain not found",
StatusCodes.NOT_FOUND,
"ChainNotFound",
);
throw error;
}

const minimizeChainData = minimizeChain(chainData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,11 @@ export async function getContractIndexedBlockRange(fastify: FastifyInstance) {
});

if (!result.fromBlock || !result.toBlock) {
const error = createCustomError(
throw createCustomError(
`No logs found for chainId: ${chainId}, contractAddress: ${standardizedContractAddress}`,
StatusCodes.NOT_FOUND,
"LOG_NOT_FOUND",
);
throw error;
}

reply.status(StatusCodes.OK).send({
Expand Down
4 changes: 3 additions & 1 deletion src/server/routes/transaction/blockchain/getUserOpReceipt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ export async function getUserOpReceipt(fastify: FastifyInstance) {
}),
});
if (!resp.ok) {
throw `Unexpected status ${resp.status} - ${await resp.text()}`;
throw new Error(
`Unexpected status ${resp.status} - ${await resp.text()}`,
);
}

const json = await resp.json();
Expand Down
6 changes: 2 additions & 4 deletions src/server/routes/transaction/getAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,17 @@ export async function getAllTx(fastify: FastifyInstance) {
const { page, limit } = request.query;

if (isNaN(parseInt(page, 10))) {
const customError = createCustomError(
throw createCustomError(
"Page must be a number",
StatusCodes.BAD_REQUEST,
"BAD_REQUEST",
);
throw customError;
} else if (isNaN(parseInt(limit, 10))) {
const customError = createCustomError(
throw createCustomError(
"Limit must be a number",
StatusCodes.BAD_REQUEST,
"BAD_REQUEST",
);
throw customError;
}

const { transactions, totalCount } = await getAllTxs({
Expand Down
6 changes: 2 additions & 4 deletions src/server/routes/transaction/getAllDeployedContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,17 @@ export async function getAllDeployedContracts(fastify: FastifyInstance) {
const { page, limit } = request.query;

if (isNaN(parseInt(page, 10))) {
const customError = createCustomError(
throw createCustomError(
"Page must be a number",
StatusCodes.BAD_REQUEST,
"BAD_REQUEST",
);
throw customError;
} else if (isNaN(parseInt(limit, 10))) {
const customError = createCustomError(
throw createCustomError(
"Limit must be a number",
StatusCodes.BAD_REQUEST,
"BAD_REQUEST",
);
throw customError;
}
const { transactions, totalCount } = await getAllTxs({
page: parseInt(page, 10),
Expand Down
3 changes: 1 addition & 2 deletions src/server/routes/transaction/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,11 @@ export async function checkTxStatus(fastify: FastifyInstance) {
const returnData = await getTxById({ queueId });

if (!returnData) {
const error = createCustomError(
throw createCustomError(
`Transaction not found with queueId ${queueId}`,
StatusCodes.NOT_FOUND,
"TX_NOT_FOUND",
);
throw error;
}

reply.status(StatusCodes.OK).send({
Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/transaction/syncRetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export async function syncRetryTransaction(fastify: FastifyInstance) {
try {
txResponse = await signer.sendTransaction(txRequest);
if (!txResponse) {
throw "Missing transaction response.";
throw new Error("Missing transaction response.");
}
} catch (e) {
const errorMessage = await parseTxError(tx, e);
Expand Down
2 changes: 1 addition & 1 deletion src/server/utils/simulateTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const simulateRaw = async (tx: InputTransaction) => {
from: tx.fromAddress,
});
} catch (e: any) {
throw e?.message || e.toString();
throw new Error(e?.message || e.toString());
}
};

Expand Down
12 changes: 7 additions & 5 deletions src/worker/queues/workers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,25 @@ export const logWorkerEvents = (worker: Worker) => {
worker.on("active", (job: Job) => {
logger({
level: "debug",
message: `Processing job ${job.id}`,
message: `[${worker.name}] Processing: ${job.id}`,
service: "worker",
});
});
worker.on("completed", (job: Job) => {
logger({
level: "debug",
message: `[${worker.name}] Job ${job.id} has completed!`,
message: `[${worker.name}] Completed: ${job.id}`,
service: "worker",
});
});
worker.on("failed", (job: Job | undefined, err: Error) => {
logger({
level: "error",
message: `[${worker.name}] Job ${job?.id ?? "?"} has failed with ${
err.message
} ${env.NODE_ENV === "development" ? err.stack : ""}`,
message: `[${worker.name}] Failed: ${
job?.id ?? "<no job ID>"
} with error: ${err.message} ${
env.NODE_ENV === "development" ? err.stack : ""
}`,
service: "worker",
});
});
Expand Down
4 changes: 3 additions & 1 deletion src/worker/tasks/webhookWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ const handleWebhook: Processor<any, void, string> = async (
}

if (resp && !resp.ok) {
throw `Received status ${resp.status} from webhook ${webhook.url}.`;
throw new Error(
`Received status ${resp.status} from webhook ${webhook.url}.`,
);
}
};

Expand Down

0 comments on commit c16b97b

Please sign in to comment.