Skip to content

Commit

Permalink
refactor: cleanup JSON error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Jan 6, 2025
1 parent d8765d2 commit 91415eb
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 37 deletions.
19 changes: 4 additions & 15 deletions src/components/CreateButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,8 @@ export const CreateButton = () => {
setBolt12Offer(undefined);
setInvoiceValid(true);
} catch (e) {
const err: unknown =
typeof e.json === "function"
? (await e.json()).error
: e;

notify("error", formatError(err));
log.warn("Fetching invoice from bol12 failed", err);
notify("error", formatError(e));
log.warn("Fetching invoice from bol12 failed", e);
return;
}
}
Expand Down Expand Up @@ -370,17 +365,11 @@ export const CreateButton = () => {
navigate("/swap/refund/" + data.id);
}
} catch (err) {
let msg = err;

if (typeof err.json === "function") {
msg = (await err.json()).error;
}

if (msg === "invalid pair hash") {
if (err === "invalid pair hash") {
setPairs(await getPairs());
notify("error", t("feecheck"));
} else {
notify("error", msg);
notify("error", err);
}
}
};
Expand Down
6 changes: 2 additions & 4 deletions src/components/SwapChecker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,7 @@ export const SwapChecker = () => {
true,
);
} catch (e) {
const err: unknown =
typeof e.json === "function" ? (await e.json()).error : e;
if (err === "swap not eligible for a cooperative claim") {
if (e === "swap not eligible for a cooperative claim") {
log.debug(
`Server did not want help claiming ${currentSwap.id}`,
);
Expand All @@ -304,7 +302,7 @@ export const SwapChecker = () => {

const msg =
"creating cooperative signature for submarine swap claim failed";
log.warn(msg, err);
log.warn(msg, e);
notify("error", msg);
}
}
Expand Down
16 changes: 6 additions & 10 deletions src/utils/claim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,18 +186,14 @@ export const createTheirPartialChainSwapSignature = async (
).toString("hex"),
};
} catch (err) {
if (typeof err.json !== "function") {
throw err;
if (err === "swap not eligible for a cooperative claim") {
log.debug(
`Backend already broadcast their claim for chain swap ${swap.id}`,
);
return undefined;
}

const errMessage = (await err.json()).error;
if (errMessage !== "swap not eligible for a cooperative claim") {
throw err;
}

log.debug(
`backend already broadcast their claim for chain swap ${swap.id}`,
);
throw err;
}

return undefined;
Expand Down
10 changes: 9 additions & 1 deletion src/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
SubmarinePairTypeTaproot,
} from "./boltzClient";
import { ECPair } from "./ecpair";
import { formatError } from "./errors";
import { ChainSwap, ReverseSwap, SomeSwap, SubmarineSwap } from "./swapCreator";

export const isIos = () =>
Expand Down Expand Up @@ -108,7 +109,14 @@ export const fetcher = async <T = unknown>(
const apiUrl = getApiUrl() + url;
const response = await fetch(apiUrl, opts);
if (!response.ok) {
return Promise.reject(response);
try {
const body = await response.json();
return Promise.reject(formatError(body));

// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (e) {
return Promise.reject(response);
}
}
return (await response.json()) as T;
};
Expand Down
9 changes: 2 additions & 7 deletions src/utils/refund.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,13 @@ const broadcastRefund = async <T extends SubmarineSwap | ChainSwap>(
}
return swap;
} catch (e) {
const errorMsg = typeof e.json === "function" ? await e.json() : e;
if (errorMsg.error === undefined) {
throw e;
}

// When the uncooperative refund transaction is not ready to be broadcast yet
// (= non-final) and the cooperative spend has been tried but failed,
// throw the error of the cooperative spend
throw errorMsg.error === "non-final" &&
throw e === "non-final" &&
txConstructionResponse.cooperativeError !== undefined
? txConstructionResponse.cooperativeError
: errorMsg.error;
: e;
}
};

Expand Down

0 comments on commit 91415eb

Please sign in to comment.