# Error Handling (Actions)

> Gracefully handling failures without blowing up the UI.

Subject: Next.js | Lesson 36 of 65 | 4 min read
Interactive lesson: https://codewithanimation.in/nextjs/actions-error-handling
Previous: [Optimistic Updates (useOptimistic)](https://codewithanimation.in/nextjs/use-optimistic.md) | Next: [Server Actions Security](https://codewithanimation.in/nextjs/actions-security.md)

## To Throw or Not To Throw

When a Next.js Server Action encounters an uncaught error, it automatically triggers the nearest React Error Boundary (`error.js`).

While this is great for catastrophic system failures, it is terrible for expected user errors (like a taken username or an invalid credit card), because it destroys the user's form UI.

## Inline Feedback

To provide a good user experience, you want the form to stay exactly where it is, preserving their typed input, while displaying a small red error message above the submit button.

To achieve this, the Server Action must *catch* the error internally and return it as a normal object.

## The Safe Return Pattern

Wrap your server logic in a standard `try/catch` block.

If the operation succeeds, return `{ success: true }`. If it fails, catch the error and return `{ error: 'Human readable message' }`.

The client component can then check `if (res.error)` and set a local React state to display the message inline.

## Common mistake: The Redirect Trap

Next.js handles routing via exceptions. When you call `redirect('/success')`, Next.js literally *throws* a `NEXT_REDIRECT` error.

If you place `redirect()` inside your `try` block, your `catch` block will catch it and swallow it! The redirect will silently fail. You must always place `redirect()` *outside* of the `try/catch` block.

## Tip: Error Serialization

You cannot return native JavaScript `Error` objects from Server Actions to the Client, because they are not serializable.

Always extract the `.message` string and return that instead.

## Try it: The Safe Return

Hover over the code lines to see how returning a standard JavaScript object prevents the dreaded Error Boundary from wiping out the user's form.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/actions-error-handling

## Check yourself

### Question 1

Why should you avoid calling `redirect()` inside a `try/catch` block in a Server Action?

- A. Because redirects are client-side only.
- B. Because `redirect()` throws a special error to navigate, which your `catch` block will accidentally swallow.
- C. Because it will cause an infinite redirect loop.
- D. Because `try/catch` disables Next.js caching.

Correct answer: B

Next.js `redirect()` works by throwing a special `NEXT_REDIRECT` error. If you catch it, Next.js doesn't know you wanted to redirect.

- Why not A: Server Actions can redirect.
- Why not C: It just won't redirect at all.
- Why not D: They are unrelated.

### Question 2

What happens if a Server Action throws an unhandled error?

- A. The browser console logs it, but the UI remains unchanged.
- B. Next.js catches it and triggers the nearest `error.js` React Error Boundary.
- C. The Next.js server crashes and must be restarted.
- D. The user is automatically redirected to a 404 page.

Correct answer: B

Uncaught exceptions in Server Actions are treated as Server Errors, triggering the nearest `error.js` file and replacing the current route segment.

- Why not A: Next.js intercepts it.
- Why not C: It only crashes that specific request.
- Why not D: It goes to an error boundary (500), not a 404 boundary.

## Remember this

- Uncaught errors in Server Actions trigger `error.js` boundaries.
- For form validation, use `try/catch` to return safe `{ error: string }` objects.
- Never return native `Error` objects to the client; they don't serialize.
- Never put `redirect()` inside a `try/catch` block, or it will be swallowed.
- Client components use the returned error string to update inline UI state.
