Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use default retry mechanism #1704

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/App/useUserInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,26 @@ type UserInfoDto = z.infer<typeof UserInfoRespons>;
type UserInfo = UserInfoDto & {
loaded: boolean;
};
function expBackoff(retryCount: number) {
return ~~((Math.random() + 0.5) * (1 << Math.min(retryCount, 8))) * 100;
}
export const useUserInfo = (): UserInfo => {
const [exhausted, setExhausted] = useState(false);
const [retries, setRetries] = useState(0);
const { data, error, isLoading, isValidating } = useSWR(
'/min-side-arbeidsgiver/api/userInfo/v1',
fetcher,
{
onErrorRetry: (error, key, config, revalidate, { retryCount }) => {
if (retryCount == 5) {
onSuccess: () => setRetries(0),
onError: (error) => {
if (retries === 5) {
Sentry.captureMessage(
`hent userInfo fra min-side-arbeidsgiver feilet med ${
error.status !== undefined
? `${error.status} ${error.statusText}`
: error
}`
);
setExhausted(true);
}
setTimeout(() => revalidate({ retryCount }), expBackoff(retryCount));
setRetries((x) => x + 1);
},
errorRetryInterval: 100,
fallbackData: {
organisasjoner: [],
digisyfoOrganisasjoner: [],
Expand All @@ -71,6 +69,7 @@ export const useUserInfo = (): UserInfo => {
}
);
const finished = error === undefined && !isLoading && !isValidating;
const exhausted = retries >= 5;
return {
...data,
altinnError: data.altinnError || error !== undefined,
Expand Down