Im trying to create a netInfo checker on my API service. This is my job to connect with an endpoint, and i need to create a netInfo check service to handle connection errors :
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 });
});
Here I go to API service to perform:
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`);
}
My netInfo checker, where i throw the error:
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);
}
And finally my handleErrorCatch:
_handleErrorCatch: async (error: any) => {
console.log('entrei aqui 1: ' + error);
throw new Error(error);
}
The problem is: my error object is not correct. console.log() gives me this message:
myerror: {"line":185244,"column":30,"sourceURL":"http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false"}
But this is my throw error:
{"type":"connection","title":"title","message":"message"}
anyone can helps me? Thanks guys!!! o/