useGo throws errors
useGo seems to throw errors if it can't find the resource.
This should be handle more gracefully instead of throwing as will crash.
/**
* handle errors for resource
* @internal
*/
export const handleResourceErrors = (to: Resource, resource: IResourceItem) => {
if (!to?.action || !to?.resource) {
throw new Error(
[useGo]: "action" or "resource" is required.
);
}
if (["edit", "show", "clone"].includes(to?.action) && !to.id) {
throw new Error(
[useGo]: [action: ${to.action}] requires an "id" for resource [resource: ${to.resource}]
,
);
}
const actionUrl = resource[to.action];
if (!actionUrl) {
throw new Error(
[useGo]: [action: ${to.action}] is not defined for [resource: ${to.resource}]
,
);
}
};5 Replies
Hi @Joe Gaffney. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Thank you for bringing this issue to attention. You're right that throwing errors in this case could lead to crashes, which is not ideal for a smooth user experience. Let's examine this based on the available information.
The code you've shared is from an internal function
handleResourceErrors
used by the useGo
hook. This function is designed to validate the input and ensure that all necessary information is provided before attempting navigation.
While the current implementation throws errors, you're correct that a more graceful error handling approach would be beneficial. However, the knowledge sources don't provide specific information about plans to change this behavior.
That said, there are a couple of ways you can work around this issue:
1. Use try-catch blocks:
You can wrap your useGo
calls in try-catch blocks to prevent crashes:
2. Check conditions before calling:
You can check the necessary conditions before calling useGo
:
It's worth noting that this behavior is likely designed to catch programming errors early. However, your suggestion for more graceful error handling is valid and could be a good improvement for the library.
If you feel strongly about this, you might want to consider opening an issue on the refine GitHub repository to suggest this improvement. The maintainers are often responsive to user feedback and suggestions for improving the developer experience.
For more information on how useGo
works and its current implementation, you can refer to the useGo documentation (https://refine.dev/docs/routing/hooks/use-go/).
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@422289470487003153> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
wise-whiteOP•2d ago
@kapa.ai I am using the Link component which uses useGo under the hood
Let me check the knowledge base for you again. One moment! :books:
----------------------
You're correct that the
<Link>
component in refine uses useGo
under the hood. Let's explore this based on the information provided in the knowledge sources.
The <Link>
component is indeed a wrapper around useGo
and provides a convenient way to create navigation links in your refine application. According to the Link documentation (https://refine.dev/docs/routing/components/link/), the <Link>
component uses routerProvider.Link
internally, which in turn leverages useGo
for navigation.
Here's an example of how you can use the <Link>
component:
You can also use it with more complex navigation options:
The <Link>
component provides a higher-level abstraction over useGo
, which means it should handle some of the error cases more gracefully. However, it's important to note that if you're passing invalid resource or action values, you might still encounter issues similar to those you mentioned with useGo
.
To avoid potential errors, make sure that:
1. The resource you're linking to is properly defined in your <Refine>
component's resources
prop.
2. The action you're specifying (e.g., "list", "show", "edit") is valid for that resource.
3. When using actions like "show" or "edit", you provide the necessary id
parameter.
If you're still encountering issues with the <Link>
component, you might want to double-check your resource definitions and ensure that all the necessary routes are properly set up.
For more detailed information on using the <Link>
component and its props, you can refer to the Link documentation (https://refine.dev/docs/routing/components/link/).
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@422289470487003153> Kapa.ai is still learning and improving, please let me know how I did by reacting below.