Skip to content

Commit 3697d88

Browse files
authored
Better response code+message when queueId cannot be found (#291)
* fix: return 4xx if tx not found * handle websockets * remove debug line
1 parent e617031 commit 3697d88

File tree

6 files changed

+28
-11
lines changed

6 files changed

+28
-11
lines changed

src/db/transactions/getTxById.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
1+
import { Static } from "@sinclair/typebox";
12
import { PrismaTransaction } from "../../schema/prisma";
3+
import { transactionResponseSchema } from "../../server/schemas/transaction";
24
import { getPrismaWithPostgresTx } from "../client";
35
import { cleanTxs } from "./cleanTxs";
46

57
interface GetTxByIdParams {
6-
pgtx?: PrismaTransaction;
78
queueId: string;
9+
pgtx?: PrismaTransaction;
810
}
911

10-
export const getTxById = async ({ pgtx, queueId }: GetTxByIdParams) => {
12+
export const getTxById = async ({
13+
pgtx,
14+
queueId,
15+
}: GetTxByIdParams): Promise<Static<
16+
typeof transactionResponseSchema
17+
> | null> => {
1118
const prisma = getPrismaWithPostgresTx(pgtx);
1219

1320
const tx = await prisma.transactions.findUnique({
1421
where: {
1522
id: queueId,
1623
},
1724
});
18-
1925
if (!tx) {
20-
// TODO: Defined error types
21-
throw new Error(`Transaction with queueId ${queueId} not found!`);
26+
return null;
2227
}
2328

2429
const [cleanedTx] = cleanTxs([tx]);

src/server/routes/transaction/status.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export async function checkTxStatus(fastify: FastifyInstance) {
9999
request.log.info(`Websocket Connection Established for ${queueId}`);
100100
findOrAddWSConnectionInSharedState(connection, queueId, request);
101101

102-
const returnData = await getTxById({ queueId: queueId });
102+
const returnData = await getTxById({ queueId });
103103

104104
const { message, closeConnection } =
105105
await getStatusMessageAndConnectionStatus(returnData);

src/server/utils/transaction.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ export const cancelTransactionAndUpdate = async ({
2020
accountAddress,
2121
}: CancelTransactionAndUpdateParams) => {
2222
const txData = await getTxById({ queueId });
23+
if (!txData) {
24+
return {
25+
message: `Transaction ${queueId} not found.`,
26+
};
27+
}
2328

2429
let error = null;
2530
let transferTransactionResult: TransactionResponse | null = null;

src/server/utils/webhook.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ const sendWebhookRequest = async (
8181
export const sendTxWebhook = async (data: TxWebookParams): Promise<void> => {
8282
try {
8383
const txData = await getTxById({ queueId: data.id });
84+
if (!txData) {
85+
throw new Error(`Transaction ${data.id} not found.`);
86+
}
8487

8588
let webhookConfig: SanitizedWebHooksSchema[] | undefined =
8689
await getWebhookConfig(WebhooksEventTypes.ALL_TX);

src/server/utils/websocket.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,16 @@ type CustomStatusAndConnectionType = {
126126
};
127127

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

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

151154
export const formatSocketMessage = async (
152-
data: Static<typeof transactionResponseSchema>,
155+
data: Static<typeof transactionResponseSchema> | null,
153156
message: string,
154157
): Promise<string> => {
155158
const returnData = JSON.stringify({
156-
result: JSON.stringify(data),
157-
queueId: data.queueId,
159+
result: data ? JSON.stringify(data) : undefined,
160+
queueId: data?.queueId,
158161
message,
159162
});
160163
return returnData;

src/worker/listeners/updateTxListener.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export const updateTxListener = async (): Promise<void> => {
3535
const returnData = await getTxById({
3636
queueId: parsedPayload.identifier,
3737
});
38+
3839
const { message, closeConnection } =
3940
await getStatusMessageAndConnectionStatus(returnData);
4041
userSubscription.socket.send(

0 commit comments

Comments
 (0)