Skip to content

Reject promise in checkError with logoutUser: false and without redirectTo causes an error #10172

@thibault-barrat

Description

@thibault-barrat

What you were expecting:
I would like to notify user when getPermissions request is failing. To do it, I catch the error in checkError and I reject promise with logoutUser: false and without redirectTo as I just want to display a notification without logout or redirect the user

What happened instead:
The notification is correctly displayed but I have a JS error:

TypeError: can't access property "startsWith", redirectTo is undefined

This comes from useLogoutIfAccessDenied:

if (logoutUser) {
logout({}, redirectTo);
} else {
if (redirectTo.startsWith('http')) {
// absolute link (e.g. https://my.oidc.server/login)
window.location.href = redirectTo;
} else {
// internal location
navigate(redirectTo);
}
}

Steps to reproduce:
Update getPermissions and checkError with following code:

getPermissions: () => {
	return Promise.reject('getPermissions error');
},
checkError: (error) => {
	if (error.status === 401 || error.status === 403) {
		return Promise.reject({ message: 'Unauthorized' });
	}
	if (error === 'getPermissions error') {
		return Promise.reject({
			message: error,
			logoutUser: false,
		});
	}
	return Promise.resolve();
},

Environment

  • React-admin version: 4.16.17
  • React version: 18.2.0
  • Stack trace (in case of a JS error):
    logoutIfAccessDenied useLogoutIfAccessDenied.ts:105
    step chunk-6GJJR47V.js:4115
    verb chunk-6GJJR47V.js:4062
    __awaiter2 chunk-6GJJR47V.js:4048
    __awaiter2 chunk-6GJJR47V.js:4030
    logoutIfAccessDenied useLogoutIfAccessDenied.ts:51
    promise callback*useLogoutIfAccessDenied/logoutIfAccessDenied< useLogoutIfAccessDenied.ts:51
    onError usePermissions.ts:57
    NotifyManager notifyManager.js:62
    notifyFn notifyManager.js:10
    flush notifyManager.js:77
    flush notifyManager.js:76
    batchedUpdates$1 React
    flush notifyManager.js:75
    promise callback*scheduleMicrotask utils.js:322
    flush notifyManager.js:74
    batch notifyManager.js:30
    dis query.js:392
    onError query.js:348
    reject2 retryer.js:67
    run2 retryer.js:132
    promise callback*run2 retryer.js:116
    run2 retryer.js:149
    promise callback*Retryer2/run2/< retryer.js:145
    promise callback*run2 retryer.js:116
    run2 retryer.js:149
    promise callback*Retryer2/run2/< retryer.js:145
    promise callback*run2 retryer.js:116
    run2 retryer.js:149
    promise callback*Retryer2/run2/< retryer.js:145
    promise callback*run2 retryer.js:116
    Retryer2 retryer.js:156
    fetch query.js:332
    executeFetch queryObserver.js:199
    onSubscribe queryObserver.js:40
    subscribe subscribable.js:16
    useBaseQuery useBaseQuery.js:60
    React 13
    workLoop scheduler.development.js:266
    flushWork scheduler.development.js:239
    performWorkUntilDeadline scheduler.development.js:533
    js scheduler.development.js:571
    js scheduler.development.js:633
    __require2 chunk-GFT2G5UO.js:18
    js index.js:6
    __require2 chunk-GFT2G5UO.js:18
    React 2
    __require2 chunk-GFT2G5UO.js:18
    js React
    __require2 chunk-GFT2G5UO.js:18
    js React
    __require2 chunk-GFT2G5UO.js:18
    <anonymous>

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions