Skip to content
This repository has been archived by the owner on Mar 16, 2019. It is now read-only.

Return a value from catch block #715

Open
iosfitness opened this issue Jul 3, 2018 · 0 comments
Open

Return a value from catch block #715

iosfitness opened this issue Jul 3, 2018 · 0 comments

Comments

@iosfitness
Copy link

iosfitness commented Jul 3, 2018

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?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant