Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to fail a request? #31

Open
scott-mueller opened this issue Dec 17, 2018 · 3 comments
Open

How to fail a request? #31

scott-mueller opened this issue Dec 17, 2018 · 3 comments

Comments

@scott-mueller
Copy link

Hi, I am using typescript with React Native and node. I need to add an interceptor to check for reachability by verifying whether the device has a network connection. How do I cause a request to fail from within the 'request' block. I have tried throwing an error but I get an 'unhanded promise rejection' error instead.

Here is my code:

//register the new interceptor
        ReachabilityFetchInterceptor._unregisterInterceptor = registerFetchIntercept({
            request: async (url: string, options: any): Promise<Array<string>> => {
                //Check whether the url matches the regexp

                //check whether react native thinks we're offline
                NetInfo.getConnectionInfo().then((connectionInfo) => {
                    if (connectionInfo.type === "NONE" || connectionInfo.type === "UNKNOWN") {
                        throw new Error("Reachability Error - Device offline");
                    }
                }).catch((err) => {                    
                    return Promise.reject(err);
                });
           },
           requestError: (error: any): Promise<void> => {
               //do something here after the error is caught 
},
...

i'd love some advice on how to proceed with this?

thanks!

@mlegenhausen
Copy link
Owner

Your current code does not return the promise. and you are using an async function. Your code should look something like this:

registerFetchIntercept({
  request: async (url: string, options: any): Promise<[string, any]> => {
    //Check whether the url matches the regexp

    //check whether react native thinks we're offline
    const connectionInfo = await NetInfo.getConnectionInfo();
    if (connectionInfo.type === "NONE" || connectionInfo.type === "UNKNOWN") {
      throw new Error("Reachability Error - Device offline");
    }

    return [url, options];
  },
  requestError: (error: any): Promise<void> => {
  }
})

Does this what you want?

@scott-mueller
Copy link
Author

scott-mueller commented Dec 24, 2018

No, I need a way to make the code enter the 'requestError; block from within the 'request' block. So far just returning an error, or rejecting the promise doesn't cause that to happen

@mlegenhausen
Copy link
Owner

mlegenhausen commented Dec 26, 2018

Try to register two times. Each registration stands for a then(request, requestError) block. So when you raise an error in request you have a second then like then(request).then(null, requestError). Also try to vary the order of the rigistrations cause internally the registration will be executed in reverse order.

registerFetchIntercept({
  requestError: (error: any): Promise<void> => {
  }
});

registerFetchIntercept({
  request: async (url: string, options: any): Promise<[string, any]> => {
    //Check whether the url matches the regexp

    //check whether react native thinks we're offline
    const connectionInfo = await NetInfo.getConnectionInfo();
    if (connectionInfo.type === "NONE" || connectionInfo.type === "UNKNOWN") {
      throw new Error("Reachability Error - Device offline");
    }

    return [url, options];
  }
});

Maybe this helps.

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

No branches or pull requests

2 participants