【问题标题】:Code Execution stops after axios 500 internal server erroraxios 500 内部服务器错误后代码执行停止
【发布时间】:2020-06-06 16:33:09
【问题描述】:

我将axiostrycatch 一起使用,但是当api 返回500 内部服务器错误时,代码执行流到catch 但在async 函数代码之外没有执行,我不知道为什么。

这是我的代码:

export async function authRequest(router, path='/', data={}, method='get') {

  const authKey = getAuthKey();

  try {
    const request = await axios({
      method,
      url: apiUrl + path,
      headers: {
        'Authorization': "Bearer " + authKey
      },
      data
    });

    return request.data;
  } catch (error) {

    const isLogged = await axios({
      method: 'get',
      url: apiUrl + '/isLoggedUser',
      headers: {
        'Authorization': "Bearer " + authKey
      }
    });

    if (isLogged.data == false) {
      router.push({
        name: 'login'
      })
    } else {
      console.error('Cannot process this request');
      return false;
    }
  }
}

调用这个函数:

const req = await authRequest(
    this.$router,
    '/video/play',
  );

console.log('cannot reach to this statment after 500 error in axios request')

Axios 引发 500 内部服务器错误时,最后一个 console.log 语句不会执行/到达或任何代码。

请注意:问题出在 axios 500 内部服务器上,它会在 authRequest 函数之外停止进一步的代码执行。

【问题讨论】:

    标签: javascript async-await axios


    【解决方案1】:

    您在 catch 块中发出的 Axios 请求应该被 try/catch 包围。 2XX 以外的任何响应都将进入 catch 块(抛出错误)。

    For more about this

    export async function authRequest(router, path = '/', data = {}, method = 'get') {
    
      const authKey = getAuthKey();
    
      try {
        const request = await axios({
          method,
          url: apiUrl + path,
          headers: {
            'Authorization': "Bearer " + authKey
          },
          data
        });
    
        return request.data;
      } catch (error) {
    
        try {
          const isLogged = await axios({
            method: 'get',
            url: apiUrl + '/isLoggedUser',
            headers: {
              'Authorization': "Bearer " + authKey
            }
          });
    
          if (isLogged.data == false) {
            router.push({
              name: 'login'
            })
          }
        } catch (e) {
          console.error('Cannot process this request');
          return false;
        }
      }
    }
    

    您可以在调用authRequest 时使用try/catch 进行尝试吗?

    try{
    
        const req = await authRequest(
        this.$router,
        '/video/play',
      );
      console.log('cannot reach to this statment after 500 error in axios request')
    }
    catch(e){
        console.log('cannot reach to this statment after 500 error in axios request')
    }
    

    【讨论】:

    • 感谢您的回答,但 catch block 中的 axios 请求不是问题,它会返回 200 status code,顺便说一下,我也尝试了您的解决方案,但仍然不在此 async authRequest函数代码未执行
    • @SyedAqeel 你能在调用authRequest时尝试使用try/catch吗
    • 是的,我在调用authRequest函数时也试过trycatch,但问题还是一样,当axios在函数内部抛出错误时,函数调用后代码不执行
    • 你在异步函数中调用authRequest吗?
    • 是的,它在异步函数中
    猜你喜欢
    • 1970-01-01
    • 2020-07-29
    • 2017-09-03
    • 1970-01-01
    • 2020-05-09
    • 1970-01-01
    • 1970-01-01
    • 2010-11-07
    • 1970-01-01
    相关资源
    最近更新 更多