Skip to content

Commit

Permalink
fix: remove delegation from local storage only when error status is 404
Browse files Browse the repository at this point in the history
  • Loading branch information
totraev committed Sep 20, 2024
1 parent 29fce5f commit 0c98aa3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/utils/local_storage/filterDelegationsLocalStorage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Delegation } from "@/app/types/delegations";

import { getTxInfo } from "../mempool_api";
import { getTxInfo, ServerError } from "../mempool_api";

// Duration after which a delegation should be removed from the local storage
// if not identified by the API or mempool.
Expand Down Expand Up @@ -44,10 +44,12 @@ export const filterDelegationsLocalStorage = async (
try {
const fetchedTx = await getTxInfo(localDelegation.stakingTxHashHex);
if (!fetchedTx) {
throw new Error("Transaction not found in the mempool");
throw new ServerError("Transaction not found in the mempool", 404);
}
} catch (err) {
if ((err as ServerError).code === 404) {
isInMempool = false;
}
} catch (_error) {
isInMempool = false;
}

if (!isInMempool) {
Expand Down
11 changes: 10 additions & 1 deletion src/utils/mempool_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ import { Fees, UTXO } from "./wallet/wallet_provider";

const { mempoolApiUrl } = getNetworkConfig();

export class ServerError extends Error {
constructor(
message: string,
public code: number,
) {
super(message);
}
}

/*
URL Construction methods
*/
Expand Down Expand Up @@ -199,7 +208,7 @@ export async function getTxInfo(txId: string): Promise<any> {
const response = await fetch(txInfoUrl(txId));
if (!response.ok) {
const err = await response.text();
throw new Error(err);
throw new ServerError(err, response.status);
}
return await response.json();
}

0 comments on commit 0c98aa3

Please sign in to comment.