Skip to content

Commit

Permalink
Merge pull request #95 from daithihearn/meaningful-error-messages
Browse files Browse the repository at this point in the history
Showing meaningful error messages
  • Loading branch information
daithihearn authored Jan 19, 2023
2 parents e529ae4 + 8075558 commit b372c89
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/components/Avatar/ProfilePictureEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -58,7 +59,7 @@ const ProfilePictureEditor: React.FC<InputsI> = ({ show, callback }) => {
}),
)
.catch((e: Error) =>
enqueueSnackbar(e.message, { variant: "error" }),
enqueueSnackbar(parseError(e), { variant: "error" }),
)
.finally(callback)
},
Expand Down
19 changes: 14 additions & 5 deletions src/components/Game/AutoActionManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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])
Expand Down
3 changes: 2 additions & 1 deletion src/components/Game/Buying.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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" }),
)
}

Expand Down
3 changes: 2 additions & 1 deletion src/components/Game/Calling.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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],
Expand Down
3 changes: 2 additions & 1 deletion src/components/Game/MyCards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
toggleAutoPlay,
clearAutoPlay,
} from "../../caches/AutoPlaySlice"
import parseError from "../../utils/ErrorUtils"

interface DoubleClickTracker {
time: number
Expand Down Expand Up @@ -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])

Expand Down
5 changes: 3 additions & 2 deletions src/components/Game/SelectSuit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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" }),
)
}
},
Expand All @@ -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" }),
)
}

Expand Down
3 changes: 2 additions & 1 deletion src/components/MyGames/MyGames.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -44,7 +45,7 @@ const MyGames = () => {
handleCloseDeleteGameModal()
})
.catch((e: Error) =>
enqueueSnackbar(e.message, { variant: "error" }),
enqueueSnackbar(parseError(e), { variant: "error" }),
)
}

Expand Down
5 changes: 3 additions & 2 deletions src/components/MyProfile/MyProfileSync.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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",
}),
)
Expand Down
3 changes: 2 additions & 1 deletion src/components/StartNewGame/StartNewGame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -69,7 +70,7 @@ const StartNewGame = () => {
})
})
.catch((e: Error) =>
enqueueSnackbar(e.message, { variant: "error" }),
enqueueSnackbar(parseError(e), { variant: "error" }),
)
},
[selectedPlayers, newGameName],
Expand Down
5 changes: 3 additions & 2 deletions src/pages/Game/Game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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" }),
)
}

Expand Down
5 changes: 3 additions & 2 deletions src/pages/Home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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" }),
)
}

Expand Down

0 comments on commit b372c89

Please sign in to comment.