0

RA/CRUD_GET_LIST_FAILURE我正在使用带有标准 crud 选项的 react-admin,如果错误是 401(例如令牌超时),我想对(以及将来的所有其他人)做出反应以注销用户

我该怎么做?在自定义请求中,我有自己的 sagas 可以在 catch 部分处理这个问题并正确地完成工作。

如果我尝试拦截RA/CRUD_GET_LIST_FAILURE这样的:

function * loadingErrorRA (action) {
  var e = action.payload;
  console.error('loadingError',action);
  if(action.error === "Unauthorized"){ 
  //I can't find a better way because I don't have access to the fetch response object in here
    yield put(userLogout());
    yield put(showNotification('Disconnected','warning'));
  } else {
    yield put(showNotification('Error '+action.error,'warning'));
  }
}

function * errorLoadingSaga() {
  yield takeLatest('RA/CRUD_GET_LIST_FAILURE', loadingErrorRA);
}

我有一个空白屏幕并弹出一个错误:

ListController.js:417 Uncaught TypeError: Cannot read property 'list' of undefined
    at Function.mapStateToProps [as mapToProps] (ListController.js:417)
    at mapToPropsProxy (wrapMapToProps.js:43)
    at handleNewPropsAndNewState (selectorFactory.js:34)
    at handleSubsequentCalls (selectorFactory.js:67)
    at pureFinalPropsSelector (selectorFactory.js:74)
    at Object.runComponentSelector [as run] (connectAdvanced.js:26)
    at Connect.componentWillReceiveProps (connectAdvanced.js:150)
    at callComponentWillReceiveProps (react-dom.development.js:11527)
   ....
index.js:2178 The above error occurred in the <Connect(TranslatedComponent(undefined))> component:
    in Connect(TranslatedComponent(undefined)) (created by List)
    in List (created by WithStyles(List))
    in WithStyles(List) (at SalesByOrganismList.js:40)
    in div (at SalesByOrganismList.js:39)
    in SalesByOrganismList (created by WithPermissions)
    in WithPermissions (created by Connect(WithPermissions))
    in Connect(WithPermissions) (created by getContext(Connect(WithPermissions)))
    ...

And then saga catch it with :
index.js:2178 uncaught at loadingErrorRA TypeError: Cannot read property 'list' of undefined
    at Function.mapStateToProps [as mapToProps] 
    ...

谢谢您的帮助

4

1 回答 1

0

https://marmelab.com/react-admin/DataProviders.html#error-format

当 API 后端返回错误时,数据提供者应该抛出一个错误对象。此对象应包含一个带有 HTTP 响应代码(404、500 等)的状态属性。React-admin 检查此错误代码,并将其用于身份验证(以防 401 或 403 错误)。此外,react-admin 在屏幕上以临时通知的形式显示错误消息。

https://marmelab.com/react-admin/Authentication.html# catch-authentication-errors-on-the-api

每次 API 返回错误时,都会使用 AUTH_ERROR 类型调用 authProvider。再一次,由您决定哪些 HTTP 状态代码应该让用户继续(通过返回已解决的承诺)或注销它们(通过返回被拒绝的承诺)。

于 2018-06-27T17:13:29.757 回答