From 8075558a4fd4aad97f23d838e49bd49370fa2dfe Mon Sep 17 00:00:00 2001 From: Daithi Hearn Date: Thu, 19 Jan 2023 15:44:46 +0100 Subject: [PATCH] Showing meaningful error messages --- .../Avatar/ProfilePictureEditor.tsx | 3 ++- src/components/Game/AutoActionManager.tsx | 19 ++++++++++++++----- src/components/Game/Buying.tsx | 3 ++- src/components/Game/Calling.tsx | 3 ++- src/components/Game/MyCards.tsx | 3 ++- src/components/Game/SelectSuit.tsx | 5 +++-- src/components/MyGames/MyGames.tsx | 3 ++- src/components/MyProfile/MyProfileSync.tsx | 5 +++-- src/components/StartNewGame/StartNewGame.tsx | 3 ++- src/pages/Game/Game.tsx | 5 +++-- src/pages/Home/Home.tsx | 5 +++-- 11 files changed, 38 insertions(+), 19 deletions(-) diff --git a/src/components/Avatar/ProfilePictureEditor.tsx b/src/components/Avatar/ProfilePictureEditor.tsx index 7fb2ba3..d678902 100644 --- a/src/components/Avatar/ProfilePictureEditor.tsx +++ b/src/components/Avatar/ProfilePictureEditor.tsx @@ -17,6 +17,7 @@ import { useAppDispatch, useAppSelector } from "../../caches/hooks" import { getMyProfile } from "../../caches/MyProfileSlice" import ProfileService from "../../services/ProfileService" import AvatarEditor from "react-avatar-editor" +import parseError from "../../utils/ErrorUtils" interface InputsI { show: boolean @@ -58,7 +59,7 @@ const ProfilePictureEditor: React.FC = ({ show, callback }) => { }), ) .catch((e: Error) => - enqueueSnackbar(e.message, { variant: "error" }), + enqueueSnackbar(parseError(e), { variant: "error" }), ) .finally(callback) }, diff --git a/src/components/Game/AutoActionManager.tsx b/src/components/Game/AutoActionManager.tsx index 79f1039..4897a39 100644 --- a/src/components/Game/AutoActionManager.tsx +++ b/src/components/Game/AutoActionManager.tsx @@ -13,9 +13,12 @@ import { import { RoundStatus } from "../../model/Round" import { getAutoPlayCard } from "../../caches/AutoPlaySlice" import { bestCardLead, getWorstCard } from "../../utils/GameUtils" +import { useSnackbar } from "notistack" +import parseError from "../../utils/ErrorUtils" const AutoActionManager = () => { const dispatch = useAppDispatch() + const { enqueueSnackbar } = useSnackbar() const gameId = useAppSelector(getGameId) const round = useAppSelector(getRound) @@ -27,8 +30,14 @@ const AutoActionManager = () => { const isMyGo = useAppSelector(getIsMyGo) const isInBunker = useAppSelector(getIsInBunker) - const playCard = (id: string, card: string) => - dispatch(GameService.playCard(id, card)).catch(console.error) + const playCard = (id: string, card: string, suppressError = false) => + dispatch(GameService.playCard(id, card)).catch(e => { + if (!suppressError) + enqueueSnackbar(parseError(e), { + variant: "error", + }) + else console.error(e) + }) const call = (id: string, callAmount: number) => dispatch(GameService.call(id, callAmount)).catch(console.error) @@ -51,11 +60,11 @@ const AutoActionManager = () => { round?.suit && round.status === RoundStatus.PLAYING ) { - if (autoPlayCard) playCard(gameId, autoPlayCard) - else if (cards.length === 1) playCard(gameId, cards[0]) + if (autoPlayCard) playCard(gameId, autoPlayCard, true) + else if (cards.length === 1) playCard(gameId, cards[0], true) else if (bestCardLead(round)) { const cardToPlay = getWorstCard(cards, round.suit) - if (cardToPlay) playCard(gameId, cardToPlay.name) + if (cardToPlay) playCard(gameId, cardToPlay.name, true) } } }, [gameId, round, isMyGo, cards, autoPlayCard]) diff --git a/src/components/Game/Buying.tsx b/src/components/Game/Buying.tsx index 11253f9..b7cafd2 100644 --- a/src/components/Game/Buying.tsx +++ b/src/components/Game/Buying.tsx @@ -18,6 +18,7 @@ import { import { riskOfMistakeBuyingCards } from "../../utils/GameUtils" import ThrowCardsWarningModal from "./ThrowCardsWarningModal" import { SelectableCard } from "../../model/Cards" +import parseError from "../../utils/ErrorUtils" const Buying = () => { const dispatch = useAppDispatch() @@ -46,7 +47,7 @@ const Buying = () => { const buyCards = (id: string, sel: SelectableCard[]) => { dispatch(GameService.buyCards(id, sel)).catch(e => - enqueueSnackbar(e.message, { variant: "error" }), + enqueueSnackbar(parseError(e), { variant: "error" }), ) } diff --git a/src/components/Game/Calling.tsx b/src/components/Game/Calling.tsx index 81b35c6..9030ac0 100644 --- a/src/components/Game/Calling.tsx +++ b/src/components/Game/Calling.tsx @@ -6,6 +6,7 @@ import { getGame, getGameId } from "../../caches/GameSlice" import { RoundStatus } from "../../model/Round" import { useCallback } from "react" import { useSnackbar } from "notistack" +import parseError from "../../utils/ErrorUtils" const Calling = () => { const dispatch = useAppDispatch() @@ -24,7 +25,7 @@ const Calling = () => { if (gameId) dispatch(GameService.call(gameId, callAmount)).catch( (e: Error) => - enqueueSnackbar(e.message, { variant: "error" }), + enqueueSnackbar(parseError(e), { variant: "error" }), ) }, [gameId], diff --git a/src/components/Game/MyCards.tsx b/src/components/Game/MyCards.tsx index 01a8913..7e386d4 100644 --- a/src/components/Game/MyCards.tsx +++ b/src/components/Game/MyCards.tsx @@ -26,6 +26,7 @@ import { toggleAutoPlay, clearAutoPlay, } from "../../caches/AutoPlaySlice" +import parseError from "../../utils/ErrorUtils" interface DoubleClickTracker { time: number @@ -139,7 +140,7 @@ const MyCards: React.FC = () => { } else { dispatch( GameService.playCard(gameId!, selectedCards[0].name), - ).catch(e => enqueueSnackbar(e.message, { variant: "error" })) + ).catch(e => enqueueSnackbar(parseError(e), { variant: "error" })) } }, [gameId, selectedCards]) diff --git a/src/components/Game/SelectSuit.tsx b/src/components/Game/SelectSuit.tsx index 997eaf6..a825912 100644 --- a/src/components/Game/SelectSuit.tsx +++ b/src/components/Game/SelectSuit.tsx @@ -14,6 +14,7 @@ import { import { removeAllFromHand } from "../../utils/GameUtils" import ThrowCardsWarningModal from "./ThrowCardsWarningModal" import { SelectableCard } from "../../model/Cards" +import parseError from "../../utils/ErrorUtils" const SelectSuit = () => { const dispatch = useAppDispatch() @@ -49,7 +50,7 @@ const SelectSuit = () => { dispatch( GameService.chooseFromDummy(gameId!, selectedCards, suit), ).catch((e: Error) => - enqueueSnackbar(e.message, { variant: "error" }), + enqueueSnackbar(parseError(e), { variant: "error" }), ) } }, @@ -63,7 +64,7 @@ const SelectSuit = () => { ) => { if (!suit) throw Error("Must provide a suit") dispatch(GameService.chooseFromDummy(id, sel, suit)).catch((e: Error) => - enqueueSnackbar(e.message, { variant: "error" }), + enqueueSnackbar(parseError(e), { variant: "error" }), ) } diff --git a/src/components/MyGames/MyGames.tsx b/src/components/MyGames/MyGames.tsx index e68e9e9..045ea89 100644 --- a/src/components/MyGames/MyGames.tsx +++ b/src/components/MyGames/MyGames.tsx @@ -26,6 +26,7 @@ import { getMyProfile } from "../../caches/MyProfileSlice" import { useSnackbar } from "notistack" import { Game, GameStatus } from "../../model/Game" import { customStyles } from "../Tables/CustomStyles" +import parseError from "../../utils/ErrorUtils" const MyGames = () => { const dispatch = useAppDispatch() @@ -44,7 +45,7 @@ const MyGames = () => { handleCloseDeleteGameModal() }) .catch((e: Error) => - enqueueSnackbar(e.message, { variant: "error" }), + enqueueSnackbar(parseError(e), { variant: "error" }), ) } diff --git a/src/components/MyProfile/MyProfileSync.tsx b/src/components/MyProfile/MyProfileSync.tsx index 045a403..332de26 100644 --- a/src/components/MyProfile/MyProfileSync.tsx +++ b/src/components/MyProfile/MyProfileSync.tsx @@ -5,6 +5,7 @@ import ProfileService from "../../services/ProfileService" import { useAuth0 } from "@auth0/auth0-react" import { useAppDispatch } from "../../caches/hooks" import { useSnackbar } from "notistack" +import parseError from "../../utils/ErrorUtils" const MyProfileSync: React.FC = () => { const dispatch = useAppDispatch() @@ -28,13 +29,13 @@ const MyProfileSync: React.FC = () => { accessToken, ), ).catch((e: Error) => - enqueueSnackbar(`Service: ${e.message}`, { + enqueueSnackbar(parseError(e), { variant: "error", }), ), ) .catch((e: Error) => - enqueueSnackbar(`AccessToken: ${e.message}`, { + enqueueSnackbar(parseError(e), { variant: "error", }), ) diff --git a/src/components/StartNewGame/StartNewGame.tsx b/src/components/StartNewGame/StartNewGame.tsx index acca049..729cc75 100644 --- a/src/components/StartNewGame/StartNewGame.tsx +++ b/src/components/StartNewGame/StartNewGame.tsx @@ -21,6 +21,7 @@ import { useAppDispatch, useAppSelector } from "../../caches/hooks" import { PlayerProfile } from "../../model/Player" import { useSnackbar } from "notistack" import { customStyles } from "../Tables/CustomStyles" +import parseError from "../../utils/ErrorUtils" const StartNewGame = () => { const dispatch = useAppDispatch() @@ -69,7 +70,7 @@ const StartNewGame = () => { }) }) .catch((e: Error) => - enqueueSnackbar(e.message, { variant: "error" }), + enqueueSnackbar(parseError(e), { variant: "error" }), ) }, [selectedPlayers, newGameName], diff --git a/src/pages/Game/Game.tsx b/src/pages/Game/Game.tsx index abd2dfd..98bffb0 100644 --- a/src/pages/Game/Game.tsx +++ b/src/pages/Game/Game.tsx @@ -14,6 +14,7 @@ import { getIsGameActive, resetGame } from "../../caches/GameSlice" import { clearAutoPlay } from "../../caches/AutoPlaySlice" import { clearMyCards } from "../../caches/MyCardsSlice" import RefreshingData from "../../components/icons/RefreshingData" +import parseError from "../../utils/ErrorUtils" const Game = () => { const dispatch = useAppDispatch() @@ -24,11 +25,11 @@ const Game = () => { const fetchData = async () => { if (id) await dispatch(GameService.refreshGameState(id)).catch((e: Error) => - enqueueSnackbar(e.message, { variant: "error" }), + enqueueSnackbar(parseError(e), { variant: "error" }), ) await dispatch(GameService.getAllPlayers()).catch((e: Error) => - enqueueSnackbar(e.message, { variant: "error" }), + enqueueSnackbar(parseError(e), { variant: "error" }), ) } diff --git a/src/pages/Home/Home.tsx b/src/pages/Home/Home.tsx index 92f1a4b..1c8b938 100644 --- a/src/pages/Home/Home.tsx +++ b/src/pages/Home/Home.tsx @@ -15,6 +15,7 @@ import { useSnackbar } from "notistack" import StatsService from "../../services/StatsService" import { Divider } from "@mui/material" import RefreshingData from "../../components/icons/RefreshingData" +import parseError from "../../utils/ErrorUtils" const Home = () => { const dispatch = useAppDispatch() @@ -24,13 +25,13 @@ const Home = () => { const fetchData = async () => { if (myProfile.isAdmin) await dispatch(GameService.getAllPlayers()).catch((e: Error) => - enqueueSnackbar(e.message, { variant: "error" }), + enqueueSnackbar(parseError(e), { variant: "error" }), ) await dispatch(GameService.getAll()) await dispatch(StatsService.gameStatsForPlayer()).catch((e: Error) => - enqueueSnackbar(e.message, { variant: "error" }), + enqueueSnackbar(parseError(e), { variant: "error" }), ) }