Debugging fetch requests

I feel like this should be simple.

I’ve noticed that when I have an error in a fetch request (bad URL, etc.), npx execute will hang indefinitely waiting for the promise to resolve. I can’t see or output an error message.

I’ve had this happen in other packs and resolved it by eyeballing the code and fixing it without any kind of debugging.

Even if I add a try/catch, it still hangs.

Here’s a simple example:

    pack.addNetworkDomain("raindrop.io");
.
.
.
    execute: async function ([], context) {
      console.log("Getting collections");
      try {
        let response = await context.fetcher.fetch({
          method: "GET",
          url: "https://api.raindrop.io/rest/v1/collections",
        });
        console.log(response.body);
        let results = [];
        for (let collection of response.body.items) {
          results.push(toCollection(collection));
        }
        return {
          result: results,
        };
      } catch (error) {
        console.log(error);
      }
    },

  • OAuth2 is configured and works

What am I missing here? How can I catch the error?

I fixed the issue by whitelisting the oauth redirect_uri.

But again, how can I debug that in the npx execute?

I’m not even close to being a NodeJS expert, but I think the deep answer to your question is in this presentation. It’s deep, but very informative about the underlying event loop in Node.

Node keeps track of the active things happening in your code, and when there is nothing else happening, it simply exits. I think this is what you may be seeing; a promise that never resolves. As such, a try-catch will not see that which is unresolved.

As I’ve gleaned from far smarter developers than muself, Node keeps a reference count of things like timers and network requests. When you make a network, or other async request, set a timer, etc. Node adds on to this ref count. When the times/request resolve, Node subtracts from the count. This count is how Node decides whether to exit at the end of the event loop. When you get to the end of the event loop Node looks at this count and if it’s zero exits.

@Bill_French Thanks for this response!

I think what baffles me is that I have to think this is a common occurrence, yet I can’t find any support discussions around this.

Anytime I have a typo in my URL, I get this issue and I have to manually dig it out.

How long does run? 300 seconds? I believe that’s the default for Chrome, but could be different depending on other browsers. And, I’m uncertain if this isn’t a server-side fetch anyway.

Still way over my head, but maybe consider this:

async function loadRaindrop() {
  try {
    const response = await fetchWithTimeout('/rest/v1/collections', {
      timeout: 6000
    });
    const games = await response.json();
    return games;
  } catch (error) {
    // Timeouts if the request takes
    // longer than 6 seconds
    console.log(error.name === 'AbortError');
  }
}

@Troy_Larson - Thanks for reporting this. I can’t seem to get npx execute to hang like you are describing, so it’s hard to know what the cause is. If you can narrow it down at all let me know.