-
-
Notifications
You must be signed in to change notification settings - Fork 281
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
feature: add way to show error in promise toast with server actions #450
Comments
In my case I didn't care that the error was displayed in the server console, my problem was that the error message was displayed correctly in development but not in production "use server" turns all functions in your actions.ts file into endpoints for the client. Therefore you must find a way to "reject" the promise with an error without using Error instance in the server action. Works in development: //actions.ts
'user server'
export async function serverAction(){
const { error, someResult } = await someDatabaseQuery();
if (error) {
throw new Error("Error:" error);
}
if (!someResult) {
throw new Error("Error message");
}
return someResult
}
//page.tsx
'use client'
toast.promise(serverAction()), {
loading: 'await response...',
success: 'response ok!',
error: (error) => error.message,
}); but in production the error message is not shown, instead it is shown: SolutionThis works for me, it just reduces generatePromise function a little, but it's still the same: //utils.ts
export async function customPromise<T>(promise: Promise<{ success: boolean; data: T }>): Promise<T> {
const { success, data } = await promise;
if (success) {
return data;
}
throw data;
} avoid generating an Error instance in the server action: 'user server'
export async function serverAction(){
const { error, someResult } = await someDatabaseQuery();
if (error) {
return { success: false, data: error.message || "Error message" };
}
if (!someResult) {
return { success: false, data: "Error message" };
}
return { success: true, data: someResult };
} on the client I use customPromise. This function generates a promise that may fail and return the error message: toast.promise(customPromise(serverAction()), {
loading: 'await response...',
success: 'response ok!', //you can get someResult
error: (message) => message,
}); Personally I would like them to include this in the documentation, but I don't think there are many use cases, since in next js server actions are not the only way to do fetching and it is not the most preferred by everyone. So this will probably never be added... |
Describe the feature / bug 📝:
it is posible to handle errror in toast.promise in other way than throwing new error [
throw new Error("MESSAGE")
] in async function?i have toast with promise function where i am deleting user, if i have error in server function i wanna have error variant of toast, it is possible with throwing error but it also give it in console etc.
function in client component
server action function
stupid sollution
it works but it pretty goofy
it is the best solution but also very goofy and long...
The text was updated successfully, but these errors were encountered: