-
Notifications
You must be signed in to change notification settings - Fork 17
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
Unable to display error message when server returns 500 with react-query #503
Comments
The closest doc I found is error handling of connect-rpc, from the doc, I learned that I need to |
Hi @sillykelvin. Error handling with react-query is a little different compared to vanilla js. Catching errors is really just like you have it, by checking the export default function Page() {
const { isPending, error, data } = useQuery(MyService.method.sayHello, { ... });
if (error) {
if (error instanceof ConnectError) {
return (
<div>Connect error: {error.message}</div>
)
}
return (
<div>{error.message}</div>
);
}
if (isPending) {
return (
<div>loading data</div>
);
}
return (
<div>{data}</div>
);
} One thing to note about react-query is that it contains default retry logic, so the above will actually only fail after a few retries (see retry option in docs). It sometimes helps to disable retry for all queries when initializing your query client: import { QueryClient } from '@tanstack/react-query'
const queryClient = new QueryClient({
defaultOptions: {
queries: {
retry: false,
},
},
}) |
Really thanks for your kind help, I never realized that it's because the retry option. After retry disabled, errors can be displayed as expected. |
I have the following code:
I want to display data if the RPC succeeds, and display error if it fails. However:
loading data
, no error message displayedI notice that the browser console reports an error, seems to be the call stack when 500 error occurs:
And the js source file link on first line (
next
function) shows the following code:It has a red wavy underline at line
const response = await fetch(...)
.I'm totally new to react/connect-rpc/react-query, I have read docs of the three and didn't find anything useful to this problem, any help is appreciated, thanks.
The text was updated successfully, but these errors were encountered: