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

Hook use-fetch throwing error to component crashes whole application #6215

Open
2 tasks done
lucastzmov opened this issue May 10, 2024 · 1 comment
Open
2 tasks done
Labels
Fixed patch Completed issues that will be published with next patch (1.0.X)

Comments

@lucastzmov
Copy link

Dependencies check up

  • I have verified that I use latest version of all @mantine/* packages

What version of @mantine/* packages do you have in package.json?

7.9.8

What package has an issue?

@mantine/hooks

What framework do you use?

create-react-app (CRA)

In which browsers you can reproduce the issue?

Chrome

Describe the bug

In the use-fetch, when returning an error, the actual flow catches the error but then throw it again, it is difficult to catch it again at component level making the whole application crash.

With the actual use-fetch hook is not possible to code component as follows:

if (loading) {
  return <Text>Loading...</Text>;
}

if (error) {
  return <Text>Oops...</Text>;
}

return (
  <>
    <div>{JSON.stringify(data)}</div>
  </>
);

If possible, include a link to a codesandbox with a minimal reproduction

https://codesandbox.io/p/devbox/react-vite-ts-mantine-hook-use-fetch-6fpzrs

Possible fix

I think it must returns the catched error to the consumer, and not throwing it again. The way it is now, when having an error in the promise, it is not returning inside the error's field but in the data one.

  • The data's field must be only when has success;
  • The error one must be the Error object returned from the promise;

Actual code handling error:

.catch((err) => {
  setLoading(false);

  if (err.name !== 'AbortError') {
    setError(err);
  }

  throw err;
});

Suggestion:

.catch((err) => {
  setLoading(false);

  if (err.name !== 'AbortError') {
    setError(err);
  }

  return err;
});

Self-service

  • I would be willing to implement a fix for this issue
@axelkovacki
Copy link

I got the same problem

@rtivital rtivital added the Fixed patch Completed issues that will be published with next patch (1.0.X) label May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fixed patch Completed issues that will be published with next patch (1.0.X)
Projects
None yet
Development

No branches or pull requests

3 participants