Skip to content

Commit c235da6

Browse files
authored
Merge pull request #62 from eresearchqut/ERP-3435_Refine_error_handling
[ERP-3435] refine error handling
2 parents 44f4c08 + e07e429 commit c235da6

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

frontend/client/fetchers.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { fetchAuthSession } from "aws-amplify/auth";
1+
import { fetchAuthSession, signOut } from "aws-amplify/auth";
22

33
export interface FetcherProps {
44
apiUrl: string;
@@ -58,25 +58,26 @@ export const buildApiEndpoint = ({
5858

5959
export const getHeaders = async () =>
6060
fetchAuthToken()
61-
.then(
62-
(idToken) =>
63-
({
64-
Authorization: `Bearer ${idToken}`,
65-
Accept: "application/json",
66-
"Content-Type": "application/json",
67-
}) as HeadersInit,
68-
)
69-
.catch((error) => {
70-
console.error(error);
71-
throw error;
61+
.then((idToken) => {
62+
if (idToken === undefined) throw Error("No idToken");
63+
return {
64+
Authorization: `Bearer ${idToken}`,
65+
Accept: "application/json",
66+
"Content-Type": "application/json",
67+
} as HeadersInit;
68+
})
69+
.catch(async () => {
70+
await signOut();
7271
});
7372

7473
export const getter = (props: FetcherProps) =>
75-
getHeaders().then((headers) =>
76-
fetch(buildApiEndpoint(props), {
77-
...props.init,
78-
headers,
79-
}).then((response) =>
80-
response.ok ? response.json() : rejectApiError(response),
81-
),
74+
getHeaders().then(
75+
(headers) =>
76+
headers &&
77+
fetch(buildApiEndpoint(props), {
78+
...props.init,
79+
headers,
80+
}).then((response) =>
81+
response.ok ? response.json() : rejectApiError(response),
82+
),
8283
);

frontend/components/errorBoundary/asyncErrorBoundary.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ const AsyncErrorBoundary: FunctionComponent<PropsWithChildren> = ({
1111
showBoundary(event);
1212
};
1313

14-
window.addEventListener("error", handleError);
1514
window.addEventListener("unhandledrejection", handleError);
1615

1716
return () => {
18-
window.removeEventListener("error", handleError);
1917
window.removeEventListener("unhandledrejection", handleError);
2018
};
2119
}, [showBoundary]);

0 commit comments

Comments
 (0)