保存传奇中没有抛出异常

Exception not thrown inside save saga

我正在使用 redux-saga 状态管理开发 SPA。我的加载和保存方法本身是有效的,但是有很多奇怪的东西......下面是传奇代码:

export function* getEventDetails({ id }) {
  const requestURL = `${url}/admin/event/${id}`
  try {
    const event = yield call(request, requestURL)
    yield put(eventLoaded(event, id))
  } catch (err) {
    yield put(eventLoadingError(err))
  }
}

export function* saveEventDetails({ event }) {
  const id = event['id']
  const requestURL = `${url}/admin/event/${
    !isNaN(id) && id !== undefined && id !== null ? id : 'new'
  }`
  try {
    const createdEvent = yield call(request, requestURL, {
      method: !isNaN(id) && id !== undefined && id !== null ? 'PUT' : 'POST',
      body: JSON.stringify(event)
    })
    yield put(eventSaved(createdEvent, createdEvent['id']))
    yield put(loadEvent(createdEvent['id']))
    yield put(loadPreviousEvents())
    yield put(loadUpcomingEvents())
  } catch (err) {
    console.log('caught error inside saga')
    yield put(eventSavingError(err))
  }
}

export default function* eventsData() {
  yield takeLatest(LOAD_EVENT, getEventDetails)
  yield takeLatest(SAVE_EVENT, saveEventDetails)
}

有一点很奇怪——如果我关闭 API 服务器然后尝试保存,我永远不会在控制台中看到 caught error inside saga。因此,我无法发送 eventSavingError 操作等

我的错误操作在哪里?在控制台中我看到:

reducer.js:48 action: {type: "project/Container/SAVE_EVENT", event: {…}}
request.js:55 PUT http://localhost:5000/event/10 net::ERR_CONNECTION_REFUSED

请求函数:

function checkStatus(response) {
  if (response.status >= 200 && response.status < 300) {
    return response
  }
  const error = new Error(response.statusText)
  error.response = response
  throw error
}

export default function request(url, options) {
  const headers = {
    Accept: 'application/json',
    'Content-Type': 'application/json',
    'Access-Control-Request-Headers': 'Content-Type, Authorization'
  }
  const token = localStorage.getItem('token')
  if (token) {
    headers['Authorization'] = `Bearer ${token}`
  }
  const newOptions = {
    ...options,
    mode: 'cors',
    headers
  }
  return fetch(url, newOptions)
    .then(checkStatus)
    .then(parseJSON)
}

根据@oozywaters 的建议,我将代码调整为:

  return fetch(url, newOptions)
    .then(checkStatus)
    .then(parseJSON)
    .catch(err => {
      throw err
    })

它确实解决了缺少异常的问题。