0
0
NextJSframework~20 mins

Error handling in server actions in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Server Action Error Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a server action throws an error?
Consider a Next.js server action that throws an error during execution. What will the client see if the error is not caught inside the action?
NextJS
export async function action() {
  throw new Error('Failed to save data');
}

export default function Page() {
  async function handleSubmit() {
    await action();
  }
  return <button onClick={handleSubmit}>Submit</button>;
}
AThe server action retries automatically until it succeeds without notifying the client.
BThe error is automatically caught and displayed as a toast notification on the client.
CThe client sees the error message 'Failed to save data' rendered inside the button element.
DThe client receives a generic 500 server response and the button click fails silently.
Attempts:
2 left
💡 Hint
Think about what happens when errors are not caught in server code called from the client.
📝 Syntax
intermediate
2:00remaining
Which server action syntax correctly catches errors?
Choose the server action code snippet that correctly catches errors and returns a custom message.
A
export async function action() {
  try {
    // some code
  } catch (e) {
    return { error: e.message };
  }
}
B
export async function action() {
  try {
    // some code
  } finally {
    return { error: 'error' };
  }
}
C
export async function action() {
  catch (e) {
    return { error: e.message };
  }
  // some code
}
D
export async function action() {
  try {
    // some code
  } catch {
    return { error: e.message };
  }
}
Attempts:
2 left
💡 Hint
Remember the correct try-catch syntax in JavaScript and how to access the error object.
state_output
advanced
2:00remaining
What is the state of the component after a server action error?
Given this Next.js component with a server action that throws an error caught and stored in state, what will be rendered after clicking the button?
NextJS
import { useState } from 'react';

export async function action() {
  throw new Error('Save failed');
}

export default function Page() {
  const [error, setError] = useState(null);

  async function handleClick() {
    try {
      await action();
    } catch (e) {
      setError(e.message);
    }
  }

  return (
    <>
      <button onClick={handleClick}>Save</button>
      {error && <p role="alert">Error: {error}</p>}
    </>
  );
}
AThe button disappears and only the error message is shown.
BThe button remains visible and the paragraph 'Error: Save failed' appears below it.
CNothing changes visually because errors in server actions do not affect client state.
DThe page reloads automatically showing a blank screen.
Attempts:
2 left
💡 Hint
Consider how React state updates after catching an error in an async function.
🔧 Debug
advanced
2:00remaining
Why does this server action error not appear on the client?
This server action throws an error, but the client never shows it. What is the most likely cause?
NextJS
export async function action() {
  throw new Error('Oops');
}

export default function Page() {
  async function handleSubmit() {
    await action();
  }
  return <button onClick={handleSubmit}>Submit</button>;
}
AThe error is not caught in the client code, so it is swallowed silently.
BNext.js disables error reporting for server actions by default.
CThe button's onClick handler is not async, so errors are ignored.
DThe server action automatically logs errors but does not send them to the client.
Attempts:
2 left
💡 Hint
Think about what happens when an async function throws and no try-catch is used.
🧠 Conceptual
expert
3:00remaining
How should you design server actions for robust error handling?
Which approach best ensures that errors in Next.js server actions are properly communicated and handled on the client?
AAvoid throwing errors in server actions; instead, always return success responses to simplify client code.
BThrow errors in server actions and rely on Next.js to automatically display error messages on the client.
CCatch errors inside the server action and return a structured error object; client checks this object to update UI.
DUse console.log in server actions to debug errors and do not send any error info to the client.
Attempts:
2 left
💡 Hint
Think about explicit error communication between server and client.