Next.js Discord

Discord Forum

Server action Error not shown on production

Unanswered
Sun bear posted this in #help-forum
Open in Discord
Sun bearOP
I have a simple server action, that throws an error like this:
throw new Error('Not found');

On my production instance in the catch block I only see this:
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.

try {
      const user = await doSomething()
    } catch (error: any) {
      console.log(error);
    }


Yet on localhost I see the Not found error.

Any ideas why? and what to do?

20 Replies

Production masks error messages so that no secrets are leaked. Give me one sec, ill send you an informational article
These are both really really good reads about server actions.
the general gist is if you want your front end to see the error you need to return not throw so

try {
      const user = await doSomething()
    } 
catch (error: any) {
      return {error: error.message}
    }
You have to very delibrately return the error message if you want to see it in prod
Sun bearOP
i see, so if i had a function getUser which returns a user but may fail, i have to check whats returned always?
i.e. check the return from the action is not an error object?
feels a little annoying
Yes. You have to intentionally return, server actions that throw are not going to let you see the error message in prod
It is annoying, but there are really good reasons to do so.
Sun bearOP
fair enough
Think of it as a route_handler, you cant just throw
Sun bearOP
true
you have to return a response
A server action is literally a route_handler, its just handled by react.
Sun bearOP
makes total sense now
On build it creates a unique endpoint that it hits.
🙂 Those articles are really really good reads though, important to understand the security implications and why its done to get a better picture.
Sun bearOP
thanks! will give them a read now! much appreciated
No problem! If your satisfied with the answer, go ahead and mark the answer so it gets closed :))