Next.js Discord

Discord Forum

Throwing errors on server actions during deployment

Answered
Jersey Wooly posted this in #help-forum
Open in Discord
Jersey WoolyOP
if (!existingSession) { throw new Error ('Session does not exist') }
This is part of my server action. However in my deployment setting,

How do I show custom error message using throw new Error in a deployment environment?

I keep getting this message instead of my custom error messages such as "Session not found".
An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance which may provide additional details about the nature of the error.
Answered by American Crow
hm i am no typescript crack. Maybe consider somth. like https://next-safe-action.dev/
View full answer

19 Replies

American Crow
https://joulev.dev/blogs/throwing-expected-errors-in-react-server-actions
TL;DR Can't throw errors in server actions, they get swallowed have to return an object with a error prop or somth similar

"use server";
 
export async function updateName(name) {
  if (!name) return { error: "Name is required" };
  await db.users.updateName(name);
}
Jersey WoolyOP
Yeah that's what I'm doing, but because I'm setting the return to a state, type script is screaming bloody murder
Argument of type '{ error: string; } | { id: string; sessionKey: string; host: JsonValue; storyTeller: JsonValue; players: JsonValue[]; hostId: string | null; scriptName: string | null; stage: GameStage; allCharacters: JsonValue; numberOfPlayers: number | null; error?: undefined; } | undefined' is not assignable to parameter of type
Even though the if clause should get rid of this
American Crow
hm i am no typescript crack. Maybe consider somth. like https://next-safe-action.dev/
Answer
Jersey WoolyOP
Thank you! Will check it out
The first link you sent really helps though, thanks!
@joulev Should be if (!joinedSession.success) and setGameSession(joinedSession.value)
Jersey WoolyOP
Oh that's before I used the workaround from your blog, but for some reason I get this type error.
  Property 'value' does not exist on type '{ success: false; error: string; }'.
@Jersey Wooly Oh that's before I used the workaround from your blog, but for some reason I get this type error.
Well, read the code inside that blog and try to understand it
Don’t copy it blindly
Jersey WoolyOP
I know it's explicitly union type of two object types
which share the success boolean
But I don't see a reason why value is not defined, especially since I haven't asserted success to false
I need to see the code after you have used the wrapper
The code above is before you use the wrapper so not helpful to debug
Jersey WoolyOP
I fixed the type error, I had to do this:

joinedSession?.success && joinedSession.value
export const JoinGameSession = createServerAction(async function (
  sessionKey: string,
) {
  const existingSession = await GetSession(sessionKey);
  if (!existingSession) {
    throw new ServerActionError('Session does not exist');
  }
  const userSession = await auth();
  if (!userSession) {
    throw new ServerActionError('User is not logged in');
  }
// rest of code
if (!!existingSession) {
    return joinedSession;
  }
});