Skip to content

Commit 0266eae

Browse files
committed
Fix unhandled promise rejection error that is no longer caught automatically as of Node.js > 15
1 parent 15ecf69 commit 0266eae

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

react-decoupled/src/components/LoginForm.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ const LoginForm = () => {
2020
useEffect(() => {
2121
auth.isLoggedIn().then((res) => {
2222
setLoggedIn(true);
23-
})
23+
}).catch((error) => {
24+
setLoggedIn(false);
25+
});
2426
}, []);
2527

2628
const handleInputChange = (event) => {

react-decoupled/src/components/NodeForm.jsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ const NodeForm = ({id, title, body, onSuccess}) => {
9090
onSuccess(data.data);
9191
}
9292
}
93+
})
94+
.catch((error) => {
95+
console.log('Error while contacting API', error);
96+
setSubmitting(false);
9397
});
9498
} catch (error) {
9599
console.log('Error while contacting API', error);

react-decoupled/src/utils/auth.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,15 @@ export function getAuthClient(config = {}) {
9191
*/
9292
async function fetchWithAuthentication(url, options) {
9393
if (!options.headers.get('Authorization')) {
94-
const oauth_token = await token();
95-
if (oauth_token) {
96-
console.log('using token', oauth_token);
97-
options.headers.append('Authorization', `Bearer ${oauth_token.access_token}`);
94+
try {
95+
const oauth_token = await token();
96+
if (oauth_token) {
97+
console.log('using token', oauth_token);
98+
options.headers.append('Authorization', `Bearer ${oauth_token.access_token}`);
99+
}
100+
} catch (error) {
101+
// Safe to swallow this error. Just means we don't have a logged in
102+
// user.
98103
}
99104
}
100105

@@ -116,7 +121,7 @@ export function getAuthClient(config = {}) {
116121
: false;
117122

118123
if (!token) {
119-
Promise.reject();
124+
return Promise.reject('empty token');
120125
}
121126

122127
const { expires_at, refresh_token } = token;
@@ -196,10 +201,15 @@ export function getAuthClient(config = {}) {
196201
* @returns {Promise}
197202
*/
198203
async function isLoggedIn() {
199-
const oauth_token = await token();
200-
if (oauth_token) {
201-
return Promise.resolve(true);
204+
try {
205+
const oauth_token = await token();
206+
if (oauth_token) {
207+
return Promise.resolve(true);
208+
}
209+
} catch (error) {
210+
return Promise.reject(error);;
202211
}
212+
203213
return Promise.reject(false);;
204214
};
205215

0 commit comments

Comments
 (0)