-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Reset game when it is unknown, improve error handling
- Loading branch information
Showing
12 changed files
with
150 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { SWRConfig } from "swr"; | ||
import * as React from "react"; | ||
|
||
import ApiError from "../common/hooks/api/helper/ApiError"; | ||
import useHandleApiError from "../common/hooks/api/useHandleApiError"; | ||
|
||
type SWRProviderProps = { | ||
children: React.ReactNode; | ||
}; | ||
|
||
/** | ||
* A provider that enables global error handling for SWR hooks | ||
*/ | ||
const SWRProvider = ({ children }: SWRProviderProps) => { | ||
const handleApiError = useHandleApiError(); | ||
|
||
const onError = React.useCallback( | ||
(e: Error) => { | ||
if (!(e instanceof ApiError)) { | ||
return; | ||
} | ||
|
||
handleApiError(e); | ||
}, | ||
[handleApiError] | ||
); | ||
|
||
return <SWRConfig value={{ onError }}>{children}</SWRConfig>; | ||
}; | ||
|
||
export default SWRProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,66 @@ | ||
import { useIntl } from "react-intl"; | ||
import { useLocation } from "wouter"; | ||
import { useSnackbar } from "notistack"; | ||
import * as React from "react"; | ||
|
||
import ApiError from "./helper/ApiError"; | ||
import useCharacter from "../useCharacter"; | ||
import useGameId from "../useGameId"; | ||
|
||
/** | ||
* A React hook that provides error handling for the ApiError | ||
* | ||
* @returns A method that handles an API error | ||
*/ | ||
const useHandleApiError = () => { | ||
const [, , deleteCharacter] = useCharacter(); | ||
const [, , deleteGameId] = useGameId(); | ||
const [, setLocation] = useLocation(); | ||
const { enqueueSnackbar } = useSnackbar(); | ||
const intl = useIntl(); | ||
|
||
return (error?: ApiError) => { | ||
if (!error) { | ||
return; | ||
} | ||
return React.useCallback( | ||
(error?: ApiError) => { | ||
if (!error) { | ||
return; | ||
} | ||
|
||
const title = error.info?.errors[0].title; | ||
|
||
enqueueSnackbar( | ||
title | ||
? title | ||
: intl.formatMessage({ | ||
if (!error.info || error.info.errors.length === 0) { | ||
/* Show general error when no error info is set. This case only | ||
* happens when the request completely failed, e.g. by a | ||
* network error. | ||
*/ | ||
enqueueSnackbar( | ||
intl.formatMessage({ | ||
defaultMessage: "Ein Fehler ist aufgetreten!", | ||
description: "unknown error snack", | ||
}), | ||
{ variant: "error" } | ||
); | ||
{ variant: "error" } | ||
); | ||
|
||
setLocation("/"); | ||
|
||
return; | ||
} | ||
|
||
const { errors } = error.info; | ||
for (let i = 0; i < errors.length; i += 1) { | ||
const err = errors[i]; | ||
|
||
/* Reset game when the game ID is unknown and redirect to the | ||
* join game start page. | ||
*/ | ||
if (err.id === "game-not-found") { | ||
deleteCharacter(); | ||
deleteGameId(); | ||
} | ||
|
||
setLocation("/"); | ||
}; | ||
enqueueSnackbar(err.title, { variant: "error" }); | ||
setLocation("/"); | ||
} | ||
}, | ||
[deleteCharacter, deleteGameId, enqueueSnackbar, intl, setLocation] | ||
); | ||
}; | ||
|
||
export default useHandleApiError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"errors": [ | ||
{ | ||
"id": "not-found", | ||
"id": "code-not-found", | ||
"status": 404, | ||
"title": "Unbekannter QR-Code." | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"errors": [ | ||
{ | ||
"id": "not-found", | ||
"id": "game-not-found", | ||
"status": 404, | ||
"title": "Unknown game" | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters