Hi all!
I'm using next 14 and supabase in my project, and I have a server action that creates a board. The procedure first uploads the user selected picture to storage and then creates the board in the database. In both cases, if supabase returns an error to me, I immediately throw it. It looks like this:
"use server";
export async function createBoard(formData: FormData) {
// ...
const { data: uploadedPictureURL, error: pictureUploadError} = await supabase.storage.from("board-pictures")
.upload(boardPicture.name, boardPicture);
if (pictureUploadError)
throw new Error(`Failed to upload picture to storage: ${pictureUploadError.message}`);
// ...
Then, I have a client component form who defines a middleman action to do input validation and show the user proper error messages:
"use client";
export function CreateBoardForm() {
// ...
async function createBoardClientAction(formData: FormData) {
try {
await createBoard(formData); // the server action defined above
}
catch (error) {
console.error(error);
setError(error);
}
}
return (
<form action={createBoardClientAction}>
<input type="text" placeholder="Board name" name="boardName" />
<input type="file" accept="image/png, image/jpeg" name="boardPicture" />
<button type="submit">Create</button>
<p>{error ? JSON.stringify(error) : "safe"}</p>
</form>
);
}
But when this error happens, the error object only has digest, not message, i.e: {"digest":"2489054772"} and I have no idea why. Many examples in the nextjs docs show throwing errors in server actions, but never using them. Also, thrown errors from form actions just totally halt your site with an error message, so im starting to think I should just return { data, error } instead of throwing. Thoughts and help are much appreciated, thanks!