Skip to content

Recommend's hooks does not handle errors #134

@axel-lcdp

Description

@axel-lcdp

Description

Recommend React's hooks does not gracefully handle errors, and errors cannot be caught in React's ErrorBoundary.

For more context :

We are using Algolia's recommend on production and staging environment.

The production environment's index works. The staging environment's index does not build as it has not enough data (less than 1'000 events).

However, when we are running our tests against the staging env, all requests fail because the index does not exists (as it cannot build). The problem, is that all errors cannot be caught and results in React throwing an error.

For instance, in useFrequentlyBoughtTogether.js line 42 :

useEffect(() => {
  setStatus('loading');
  getFrequentlyBoughtTogether({
    // ...
  })
    .then((response) => {
      setResult(response);
      setStatus('idle');
    })
    // missing catch, resulting in error not being catch by React
}, [
  // ...
]);

It would be great to gracefully handles errors in each hook.

Reproduction

The linked codesandbox does not work. It results with an error /app.tsx: Unexpected token (133:105).

Steps

To reproduce the error, you may :

  1. Configure recommendClient with a wrong client
  2. Use an index that does not build
  3. Ask for a non-existent objectIDs

Expected behavior

Error should be gracefully handled by React / hooks.

Environment

  • OS: macOS
  • Browser: Chrome
  • Library version: 4.14.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions