Skip to content

Commit

Permalink
fix: error handling in find my combos, closes #596
Browse files Browse the repository at this point in the history
  • Loading branch information
ldeluigi committed Jan 7, 2025
1 parent 0e9c8ad commit b412792
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/pages/find-my-combos.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,17 @@ const FindMyCombos: React.FC = () => {
const cardListFromUrlApi = new CardListFromUrlApi(configuration);
const cardListFromTextApi = new CardListFromTextApi(configuration);

const handleError = async (err: any) => {
const handleFindMyCombosError = async (err: any) => {
const error = err as ResponseError;
const body = JSON.parse(await error.response.text());
const errorMessages: string[] = [];
if (Array.isArray(body.main)) {
body.main.forEach((message: string) => {
if (typeof message === 'string') {
errorMessages.push(message);
}
});
}
Object.keys(body.main).forEach((key) => {
if (body.main[key].card) {
errorMessages.push(`Card #${key}: ${body.main[key].card}`);
Expand Down Expand Up @@ -144,7 +151,7 @@ const FindMyCombos: React.FC = () => {
setDecklistErrors([]);
return decklist;
} catch (err: any) {
await handleError(err);
await handleFindMyCombosError(err);
return new Decklist({ commanders: [], main: [] });
}
};
Expand All @@ -157,7 +164,8 @@ const FindMyCombos: React.FC = () => {
setLookupInProgress(true);

if (decklist.isEmpty()) {
return setLookupInProgress(false);
setLookupInProgress(false);
return;
}

localStorage.setItem(LOCAL_STORAGE_DECK_STORAGE_KEY, JSON.stringify(DeckToJSON(decklist.deck)));
Expand Down Expand Up @@ -201,7 +209,7 @@ const FindMyCombos: React.FC = () => {

setResults(newResults);
} catch (error: any) {
await handleError(error);
await handleFindMyCombosError(error);
setResults(DEFAULT_RESULTS);
}

Expand Down

0 comments on commit b412792

Please sign in to comment.