SGrondin / bottleneck

Job scheduler and rate limiter, supports Clustering
MIT License
1.82k stars 75 forks source link

When fetch fails, should I throw a BottleneckError to fail a job? #135

Closed rhaksw closed 4 years ago

rhaksw commented 4 years ago

Hi, I'm evaluating Bottleneck and have this question after reading the readme.

Sometimes fetch fails, and I catch that error, as follows,

const handleFetchErrors = (response) => {
    if (! response.ok) {
        throw Error(response.statusText)
    }
    return response.json()
}

const fetchUrl = () => {
    return window.fetch("https://example.com")
    .then(handleFetchErrors)
    .catch(console.log)
}

fetchUrl()
.then(result => {
    if (result === undefined) return; // fetch failed
    // ... do work
})

It might be convenient to retry fetchUrl jobs that fail. Could/should this be implemented with Bottleneck as follows?

const fetchUrlWrapped = limiter.wrap(fetchUrl)

fetchUrlWrapped()
.then(result => {
    if (result === undefined) throw Bottleneck.BottleneckError();
    // ... do work
})

// Listen to the "failed" event
limiter.on("failed", async (error, jobInfo) => {
  const id = jobInfo.options.id;
  console.warn(`Job ${id} failed: ${error}`);

  if (jobInfo.retryCount === 0) { // Here we only retry once
    console.log(`Retrying job ${id} in 25ms!`);
    return 25;
  }
});

Also, do people ever use Bottleneck in extensions? I'm wondering whether or not it's a good idea to use this in an extension with a persistent background page.

SGrondin commented 4 years ago

Hi, thank you for trying it out.

It might be convenient to retry fetchUrl jobs that fail Could/should this be implemented with Bottleneck as follows?

Yup the code looks good, but you don't have to throw a BottleneckError. Any kind of error will work fine. BottleneckError is what Bottleneck throws to let you, the user, tell apart which errors came from your code and which errors came from Bottleneck itself.

Also, do people ever use Bottleneck in extensions?

Yup! I know it's used in at least a few, including one in VS Code (which uses Chromium under the hood).

rhaksw commented 4 years ago

Thank you! I will give this a shot soon.