Skip to content

Commit 8075558

Browse files
committedJan 19, 2023
Showing meaningful error messages
1 parent e529ae4 commit 8075558

File tree

11 files changed

+38
-19
lines changed

11 files changed

+38
-19
lines changed
 

‎src/components/Avatar/ProfilePictureEditor.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { useAppDispatch, useAppSelector } from "../../caches/hooks"
1717
import { getMyProfile } from "../../caches/MyProfileSlice"
1818
import ProfileService from "../../services/ProfileService"
1919
import AvatarEditor from "react-avatar-editor"
20+
import parseError from "../../utils/ErrorUtils"
2021

2122
interface InputsI {
2223
show: boolean
@@ -58,7 +59,7 @@ const ProfilePictureEditor: React.FC<InputsI> = ({ show, callback }) => {
5859
}),
5960
)
6061
.catch((e: Error) =>
61-
enqueueSnackbar(e.message, { variant: "error" }),
62+
enqueueSnackbar(parseError(e), { variant: "error" }),
6263
)
6364
.finally(callback)
6465
},

‎src/components/Game/AutoActionManager.tsx

+14-5
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ import {
1313
import { RoundStatus } from "../../model/Round"
1414
import { getAutoPlayCard } from "../../caches/AutoPlaySlice"
1515
import { bestCardLead, getWorstCard } from "../../utils/GameUtils"
16+
import { useSnackbar } from "notistack"
17+
import parseError from "../../utils/ErrorUtils"
1618

1719
const AutoActionManager = () => {
1820
const dispatch = useAppDispatch()
21+
const { enqueueSnackbar } = useSnackbar()
1922

2023
const gameId = useAppSelector(getGameId)
2124
const round = useAppSelector(getRound)
@@ -27,8 +30,14 @@ const AutoActionManager = () => {
2730
const isMyGo = useAppSelector(getIsMyGo)
2831
const isInBunker = useAppSelector(getIsInBunker)
2932

30-
const playCard = (id: string, card: string) =>
31-
dispatch(GameService.playCard(id, card)).catch(console.error)
33+
const playCard = (id: string, card: string, suppressError = false) =>
34+
dispatch(GameService.playCard(id, card)).catch(e => {
35+
if (!suppressError)
36+
enqueueSnackbar(parseError(e), {
37+
variant: "error",
38+
})
39+
else console.error(e)
40+
})
3241

3342
const call = (id: string, callAmount: number) =>
3443
dispatch(GameService.call(id, callAmount)).catch(console.error)
@@ -51,11 +60,11 @@ const AutoActionManager = () => {
5160
round?.suit &&
5261
round.status === RoundStatus.PLAYING
5362
) {
54-
if (autoPlayCard) playCard(gameId, autoPlayCard)
55-
else if (cards.length === 1) playCard(gameId, cards[0])
63+
if (autoPlayCard) playCard(gameId, autoPlayCard, true)
64+
else if (cards.length === 1) playCard(gameId, cards[0], true)
5665
else if (bestCardLead(round)) {
5766
const cardToPlay = getWorstCard(cards, round.suit)
58-
if (cardToPlay) playCard(gameId, cardToPlay.name)
67+
if (cardToPlay) playCard(gameId, cardToPlay.name, true)
5968
}
6069
}
6170
}, [gameId, round, isMyGo, cards, autoPlayCard])

‎src/components/Game/Buying.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
import { riskOfMistakeBuyingCards } from "../../utils/GameUtils"
1919
import ThrowCardsWarningModal from "./ThrowCardsWarningModal"
2020
import { SelectableCard } from "../../model/Cards"
21+
import parseError from "../../utils/ErrorUtils"
2122

2223
const Buying = () => {
2324
const dispatch = useAppDispatch()
@@ -46,7 +47,7 @@ const Buying = () => {
4647

4748
const buyCards = (id: string, sel: SelectableCard[]) => {
4849
dispatch(GameService.buyCards(id, sel)).catch(e =>
49-
enqueueSnackbar(e.message, { variant: "error" }),
50+
enqueueSnackbar(parseError(e), { variant: "error" }),
5051
)
5152
}
5253

‎src/components/Game/Calling.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { getGame, getGameId } from "../../caches/GameSlice"
66
import { RoundStatus } from "../../model/Round"
77
import { useCallback } from "react"
88
import { useSnackbar } from "notistack"
9+
import parseError from "../../utils/ErrorUtils"
910

1011
const Calling = () => {
1112
const dispatch = useAppDispatch()
@@ -24,7 +25,7 @@ const Calling = () => {
2425
if (gameId)
2526
dispatch(GameService.call(gameId, callAmount)).catch(
2627
(e: Error) =>
27-
enqueueSnackbar(e.message, { variant: "error" }),
28+
enqueueSnackbar(parseError(e), { variant: "error" }),
2829
)
2930
},
3031
[gameId],

‎src/components/Game/MyCards.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
toggleAutoPlay,
2727
clearAutoPlay,
2828
} from "../../caches/AutoPlaySlice"
29+
import parseError from "../../utils/ErrorUtils"
2930

3031
interface DoubleClickTracker {
3132
time: number
@@ -139,7 +140,7 @@ const MyCards: React.FC = () => {
139140
} else {
140141
dispatch(
141142
GameService.playCard(gameId!, selectedCards[0].name),
142-
).catch(e => enqueueSnackbar(e.message, { variant: "error" }))
143+
).catch(e => enqueueSnackbar(parseError(e), { variant: "error" }))
143144
}
144145
}, [gameId, selectedCards])
145146

‎src/components/Game/SelectSuit.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
import { removeAllFromHand } from "../../utils/GameUtils"
1515
import ThrowCardsWarningModal from "./ThrowCardsWarningModal"
1616
import { SelectableCard } from "../../model/Cards"
17+
import parseError from "../../utils/ErrorUtils"
1718

1819
const SelectSuit = () => {
1920
const dispatch = useAppDispatch()
@@ -49,7 +50,7 @@ const SelectSuit = () => {
4950
dispatch(
5051
GameService.chooseFromDummy(gameId!, selectedCards, suit),
5152
).catch((e: Error) =>
52-
enqueueSnackbar(e.message, { variant: "error" }),
53+
enqueueSnackbar(parseError(e), { variant: "error" }),
5354
)
5455
}
5556
},
@@ -63,7 +64,7 @@ const SelectSuit = () => {
6364
) => {
6465
if (!suit) throw Error("Must provide a suit")
6566
dispatch(GameService.chooseFromDummy(id, sel, suit)).catch((e: Error) =>
66-
enqueueSnackbar(e.message, { variant: "error" }),
67+
enqueueSnackbar(parseError(e), { variant: "error" }),
6768
)
6869
}
6970

‎src/components/MyGames/MyGames.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { getMyProfile } from "../../caches/MyProfileSlice"
2626
import { useSnackbar } from "notistack"
2727
import { Game, GameStatus } from "../../model/Game"
2828
import { customStyles } from "../Tables/CustomStyles"
29+
import parseError from "../../utils/ErrorUtils"
2930

3031
const MyGames = () => {
3132
const dispatch = useAppDispatch()
@@ -44,7 +45,7 @@ const MyGames = () => {
4445
handleCloseDeleteGameModal()
4546
})
4647
.catch((e: Error) =>
47-
enqueueSnackbar(e.message, { variant: "error" }),
48+
enqueueSnackbar(parseError(e), { variant: "error" }),
4849
)
4950
}
5051

‎src/components/MyProfile/MyProfileSync.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import ProfileService from "../../services/ProfileService"
55
import { useAuth0 } from "@auth0/auth0-react"
66
import { useAppDispatch } from "../../caches/hooks"
77
import { useSnackbar } from "notistack"
8+
import parseError from "../../utils/ErrorUtils"
89

910
const MyProfileSync: React.FC = () => {
1011
const dispatch = useAppDispatch()
@@ -28,13 +29,13 @@ const MyProfileSync: React.FC = () => {
2829
accessToken,
2930
),
3031
).catch((e: Error) =>
31-
enqueueSnackbar(`Service: ${e.message}`, {
32+
enqueueSnackbar(parseError(e), {
3233
variant: "error",
3334
}),
3435
),
3536
)
3637
.catch((e: Error) =>
37-
enqueueSnackbar(`AccessToken: ${e.message}`, {
38+
enqueueSnackbar(parseError(e), {
3839
variant: "error",
3940
}),
4041
)

‎src/components/StartNewGame/StartNewGame.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { useAppDispatch, useAppSelector } from "../../caches/hooks"
2121
import { PlayerProfile } from "../../model/Player"
2222
import { useSnackbar } from "notistack"
2323
import { customStyles } from "../Tables/CustomStyles"
24+
import parseError from "../../utils/ErrorUtils"
2425

2526
const StartNewGame = () => {
2627
const dispatch = useAppDispatch()
@@ -69,7 +70,7 @@ const StartNewGame = () => {
6970
})
7071
})
7172
.catch((e: Error) =>
72-
enqueueSnackbar(e.message, { variant: "error" }),
73+
enqueueSnackbar(parseError(e), { variant: "error" }),
7374
)
7475
},
7576
[selectedPlayers, newGameName],

‎src/pages/Game/Game.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { getIsGameActive, resetGame } from "../../caches/GameSlice"
1414
import { clearAutoPlay } from "../../caches/AutoPlaySlice"
1515
import { clearMyCards } from "../../caches/MyCardsSlice"
1616
import RefreshingData from "../../components/icons/RefreshingData"
17+
import parseError from "../../utils/ErrorUtils"
1718

1819
const Game = () => {
1920
const dispatch = useAppDispatch()
@@ -24,11 +25,11 @@ const Game = () => {
2425
const fetchData = async () => {
2526
if (id)
2627
await dispatch(GameService.refreshGameState(id)).catch((e: Error) =>
27-
enqueueSnackbar(e.message, { variant: "error" }),
28+
enqueueSnackbar(parseError(e), { variant: "error" }),
2829
)
2930

3031
await dispatch(GameService.getAllPlayers()).catch((e: Error) =>
31-
enqueueSnackbar(e.message, { variant: "error" }),
32+
enqueueSnackbar(parseError(e), { variant: "error" }),
3233
)
3334
}
3435

‎src/pages/Home/Home.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { useSnackbar } from "notistack"
1515
import StatsService from "../../services/StatsService"
1616
import { Divider } from "@mui/material"
1717
import RefreshingData from "../../components/icons/RefreshingData"
18+
import parseError from "../../utils/ErrorUtils"
1819

1920
const Home = () => {
2021
const dispatch = useAppDispatch()
@@ -24,13 +25,13 @@ const Home = () => {
2425
const fetchData = async () => {
2526
if (myProfile.isAdmin)
2627
await dispatch(GameService.getAllPlayers()).catch((e: Error) =>
27-
enqueueSnackbar(e.message, { variant: "error" }),
28+
enqueueSnackbar(parseError(e), { variant: "error" }),
2829
)
2930

3031
await dispatch(GameService.getAll())
3132

3233
await dispatch(StatsService.gameStatsForPlayer()).catch((e: Error) =>
33-
enqueueSnackbar(e.message, { variant: "error" }),
34+
enqueueSnackbar(parseError(e), { variant: "error" }),
3435
)
3536
}
3637

0 commit comments

Comments
 (0)