Skip to content

Commit

Permalink
use default retry mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
kenglxn committed Oct 11, 2023
1 parent d2f120d commit 9658c33
Showing 1 changed file with 7 additions and 8 deletions.
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

0 comments on commit 9658c33

Please sign in to comment.