简体   繁体   English

使用 try and catch 处理状态 500

[英]Handling status 500 with try and catch

I can't tell what is the optimal way to handle the status 500 sent by my API.我不知道处理我的 API 发送的状态 500 的最佳方法是什么。 Could you please help me?请你帮助我好吗? In the case below, when my API responds with a status 500, I wish to set an error message in my redux store.在下面的情况下,当我的 API 响应状态为 500 时,我希望在我的 redux 存储中设置错误消息。 When my API sent a successful status code, I wish to "Fetch the user decks" in my store.当我的 API 发送成功状态码时,我希望在我的商店中“获取用户套牌”。

My first attempt felt logical and dry to me, but it did not work:我的第一次尝试对我来说既合乎逻辑又枯燥,但它没有奏效:

const createDeck = async () => {
        try {
          const request = await fetch(`${back}/deck/`, options)
          const response = await request.json()
          store.dispatch({ type: FETCH_USER_DECKS })
        } catch (error) {
          store.dispatch({ type: SET_ERROR, message: error })
        }
      }

When the API send a 500 status, no exception seems to be thrown and the code in my catch block is ignored.当 API 发送 500 状态时,似乎没有抛出异常,并且我的 catch 块中的代码被忽略。

My second and third attempt worked as I excepted them too, but they feel convoluted and weird:我的第二次和第三次尝试也奏效了,但我也排除了它们,但它们感觉令人费解和奇怪:

2nd attempt:第二次尝试:

 const createDeck = async () => {
        try {
          const request = await fetch(`${back}/deck/`, options)
          const response = await request.json()
          if (request.status === 201 || request.status === 200) {
            store.dispatch({ type: FETCH_USER_DECKS })
          } else {
          store.dispatch({ type: SET_ERROR, message: error })
          }
        } catch (error) {
          console.log('error')
        }
  }

This works, but it ignores completely the catch block, making me wonder: what's even the point of try...catch then?这可行,但它完全忽略了 catch 块,这让我想知道:那么 try...catch 有什么意义呢?

Third attempt:第三次尝试:

  const createDeck = async () => {
    try {
      const request = await fetch(`${back}/deck/`, options)
      const response = await request.json()
      if (request.status === 201 || request.status === 200) {
        store.dispatch({ type: FETCH_USER_DECKS })
      } else {
        throw response
      }
    } catch (error) {
      store.dispatch({ type: SET_ERROR, message: error })
    }
  }

This third attempt make use of catch, but it feels weird to manually throw an exception, to be immediately caught by catch.这第三次尝试使用了catch,但是手动抛出异常感觉很奇怪,立即被catch捕获。

Am I missing something here?我在这里错过了什么吗?

Isbn has properly answered this in the comments. Isbn 在评论中正确回答了这个问题。 I'm putting it here again for visibility: "Since you're using fetch() it would probably make sense to adopt its implementation model and to not consider a 500 response as an exception (attempt 2). But I wouldn't see anything wrong with your third attempt either (axios, for example, would raise an error in that case). Note that fetch() response exposes a ok property to check whether the request succeeded or not"我再次把它放在这里是为了可见性:“由于您使用的是 fetch(),因此采用其实现 model 并且不将 500 响应视为异常(尝试 2)可能是有意义的。但我看不到您的第三次尝试也有任何问题(例如,axios 在这种情况下会引发错误)。请注意 fetch() 响应公开了一个 ok 属性以检查请求是否成功“

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM