Skip to content

Commit

Permalink
Better response code+message when queueId cannot be found (#291)
Browse files Browse the repository at this point in the history
* fix: return 4xx if tx not found

* handle websockets

* remove debug line
  • Loading branch information
arcoraven authored Nov 3, 2023
1 parent e617031 commit 3697d88
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 11 deletions.
15 changes: 10 additions & 5 deletions src/db/transactions/getTxById.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import { Static } from "@sinclair/typebox";
import { PrismaTransaction } from "../../schema/prisma";
import { transactionResponseSchema } from "../../server/schemas/transaction";
import { getPrismaWithPostgresTx } from "../client";
import { cleanTxs } from "./cleanTxs";

interface GetTxByIdParams {
pgtx?: PrismaTransaction;
queueId: string;
pgtx?: PrismaTransaction;
}

export const getTxById = async ({ pgtx, queueId }: GetTxByIdParams) => {
export const getTxById = async ({
pgtx,
queueId,
}: GetTxByIdParams): Promise<Static<
typeof transactionResponseSchema
> | null> => {
const prisma = getPrismaWithPostgresTx(pgtx);

const tx = await prisma.transactions.findUnique({
where: {
id: queueId,
},
});

if (!tx) {
// TODO: Defined error types
throw new Error(`Transaction with queueId ${queueId} not found!`);
return null;
}

const [cleanedTx] = cleanTxs([tx]);
Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/transaction/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export async function checkTxStatus(fastify: FastifyInstance) {
request.log.info(`Websocket Connection Established for ${queueId}`);
findOrAddWSConnectionInSharedState(connection, queueId, request);

const returnData = await getTxById({ queueId: queueId });
const returnData = await getTxById({ queueId });

const { message, closeConnection } =
await getStatusMessageAndConnectionStatus(returnData);
Expand Down
5 changes: 5 additions & 0 deletions src/server/utils/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export const cancelTransactionAndUpdate = async ({
accountAddress,
}: CancelTransactionAndUpdateParams) => {
const txData = await getTxById({ queueId });
if (!txData) {
return {
message: `Transaction ${queueId} not found.`,
};
}

let error = null;
let transferTransactionResult: TransactionResponse | null = null;
Expand Down
3 changes: 3 additions & 0 deletions src/server/utils/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ const sendWebhookRequest = async (
export const sendTxWebhook = async (data: TxWebookParams): Promise<void> => {
try {
const txData = await getTxById({ queueId: data.id });
if (!txData) {
throw new Error(`Transaction ${data.id} not found.`);
}

let webhookConfig: SanitizedWebHooksSchema[] | undefined =
await getWebhookConfig(WebhooksEventTypes.ALL_TX);
Expand Down
13 changes: 8 additions & 5 deletions src/server/utils/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,16 @@ type CustomStatusAndConnectionType = {
};

export const getStatusMessageAndConnectionStatus = async (
data: Static<typeof transactionResponseSchema>,
data: Static<typeof transactionResponseSchema> | null,
): Promise<CustomStatusAndConnectionType> => {
let message =
"Request is queued. Waiting for transaction to be picked up by worker.";
let closeConnection = false;

if (data.status === TransactionStatusEnum.Mined) {
if (!data) {
message = `Transaction not found. Make sure the provided ID is correct.`;
closeConnection = true;
} else if (data.status === TransactionStatusEnum.Mined) {
message = "Transaction mined. Closing connection.";
closeConnection = true;
} else if (data.status === TransactionStatusEnum.Errored) {
Expand All @@ -149,12 +152,12 @@ export const getStatusMessageAndConnectionStatus = async (
};

export const formatSocketMessage = async (
data: Static<typeof transactionResponseSchema>,
data: Static<typeof transactionResponseSchema> | null,
message: string,
): Promise<string> => {
const returnData = JSON.stringify({
result: JSON.stringify(data),
queueId: data.queueId,
result: data ? JSON.stringify(data) : undefined,
queueId: data?.queueId,
message,
});
return returnData;
Expand Down
1 change: 1 addition & 0 deletions src/worker/listeners/updateTxListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const updateTxListener = async (): Promise<void> => {
const returnData = await getTxById({
queueId: parsedPayload.identifier,
});

const { message, closeConnection } =
await getStatusMessageAndConnectionStatus(returnData);
userSubscription.socket.send(
Expand Down

0 comments on commit 3697d88

Please sign in to comment.