抛出错误的打字稿失败

时间:2019-11-27 21:01:20

标签: typescript react-native promise try-catch

我正在尝试在我的API服务上创建一个netInfo检查器。这是我与端点连接的工作,我需要创建一个netInfo检查服务来处理连接错误:

loginExists(username)
      .then((status: string) => {
        setLoading(false);
        if (status == '200' || status) {
          const checkSAPData = checkUserSAPMock(username);
          if (checkSAPData) {
            navigation.navigate('LoginPassword', { document: username, sapData: checkSAPData });
          } else {
            navigation.navigate('LoginPassword', { document: username });
          }
        }
      })
      .catch(error => {
        console.log('myerror: ' + JSON.stringify(error));
        setLoading(false);
        const checkSAPData = checkUserSAPMock(username);

        if (checkSAPData) {
          if (checkSAPData.Motorista.item.Autorizacao === "S")
            navigation.navigate('SignUp', { data: checkSAPData });
          else
            navigation.navigate('LoginPassword', { document: username, sapData: checkSAPData });
        } else
          navigation.navigate('SignUp', { isNewUser: true, document: cpf });
      }); 

在这里我去API服务执行:

export function loginExists(username: string) {
  const headers = new Headers({
    'Content-Type': 'application/json',
  });

  // TODO: REMOVE
  // debug
  // if (username === '00000000000') {
  // return API.get(`motoristas/cpf/09503054990/existe`);
  // }
  return API.get(`motoristas/cpf/${username}/existe`);
}

我的netInfo检查器,我在其中抛出错误:

get(endpoint: string, headersProp?: Headers | string[][] | Record<string, string>): Promise<any> {

    return ConnectionChecker.getConnection().then(state => {

      let headers = {
      };

      if (headersProp) {
        headers = { ...headers, ...headersProp };
      }

      if (state.isConnected) {
        return fetch(`${this.url}/${endpoint}`, {
          method: 'GET',
          headers
        });
      } else {

        //Criar arquivo de constantes com erros da aplicacao/genericos.
        const errorData = {
          type: 'connection',
          title: 'title',
          message: 'message'
        };

        throw new Error(JSON.stringify(errorData));
      }


    }).then(this._handleError)
      .then(this._handleContentType)
      .catch(this._handleErrorCatch);
  }

最后是我的handleErrorCatch:

_handleErrorCatch: async (error: any) => {
    console.log('entrei aqui 1: ' + error);
    throw new Error(error);
  }

问题是:我的错误对象不正确。 console.log()给我以下消息:

myerror: {"line":185244,"column":30,"sourceURL":"http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false"}

但这是我的抛出错误:

{"type":"connection","title":"title","message":"message"}

有人可以帮助我吗?谢谢你们!!! o /

0 个答案:

没有答案