wkh237/react-native-fetch-blob

Return a value from catch block

iosfitness opened this issue · 0 comments

I need to get a callback when I encounter an error from the catch block.

Installed library and RN project : "react-native-fetch-blob": "^0.10.8",

export function postRequestWithUrl(postUrl, body) {
	return NetInfo.getConnectionInfo().then((connectionInfo) => {
		if (connectionInfo.type == 'none' || connectionInfo.type == 'unknown') {
			return { 'error': GeneralPref.getNoInternetMessage }
		}
		return RNFetchBlob.fetch('POST', postUrl, {
			'Content-Type': 'application/json',
		}, JSON.stringify(body)).then((res) => {
			if (res.respInfo.status == 200) {
				let responseJson = JSON.parse(res.data);
				return responseJson;
			} else {
				return { 'error':  responseJson.message ? responseJson.message : `Error code : ${res.respInfo.status} \n Something went wrong. Please try again.` }
			}
		}).catch((err) => {
			return { 'error': JSON.stringify(err) }
		})
	});
}

Usage

 api.postRequestWithUrl(`${api.BASE_URL}/api/v1/login/app`, loginRequest).then(
      (responseJson) => {
    
        if (responseJson && !responseJson.error) {
      
        } else {
          Alert.alert('', responseJson.error)
        }
      }
    )

The catch block is entered in a request timeout condition

Any inputs on how I can achieve it?. Any better approach or already existing mechanism to handle this?