0
0
NextJSframework~20 mins

Server-side error handling in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Next.js Server Error Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Next.js server action error handler?

Consider this Next.js server action that throws an error. What will the client receive?

NextJS
export async function action() {
  try {
    throw new Error('Failed to save data')
  } catch (error) {
    return { success: false, message: error.message }
  }
}
A{"success": false, "message": "Failed to save data"}
BThe client receives a 500 Internal Server Error response with no JSON body
CThe client receives a JSON with { success: true }
DThe server crashes and restarts
Attempts:
2 left
💡 Hint

Look at what the catch block returns.

component_behavior
intermediate
2:00remaining
How does Next.js handle thrown errors in server components?

If a server component throws an error during rendering, what happens?

NextJS
export default function ServerComponent() {
  throw new Error('Render failed')
  return <div>Should not render</div>
}
AThe server silently ignores the error and renders an empty div
BThe component renders the error message inside the UI automatically
CNext.js shows a built-in error overlay in development and a 500 error page in production
DThe server restarts automatically without showing any error
Attempts:
2 left
💡 Hint

Think about Next.js default error handling behavior in dev and prod.

🔧 Debug
advanced
2:00remaining
Why does this Next.js API route fail to send a JSON error response?

Examine this API route code. Why does the client get a 500 error with no JSON?

NextJS
export default function handler(req, res) {
  try {
    throw new Error('Oops')
  } catch {
    res.status(500).json({ error: 'Server error' })
    return;
  }
  res.json({ success: true })
}
ABecause the function is missing async keyword
BBecause the catch block does not set the status code
CBecause the error is not re-thrown after catch
DBecause res.json is called twice, causing an error after the catch block
Attempts:
2 left
💡 Hint

Look at the flow after the catch block.

📝 Syntax
advanced
2:00remaining
Which option correctly throws a custom error in a Next.js server action?

Choose the code that correctly throws a custom error with a message and status code.

Athrow new Error('Not found') with status = 404
Bconst err = new Error('Not found'); err.status = 404; throw err
Cthrow new Error({ message: 'Not found', status: 404 })
Dthrow 'Not found' with status 404
Attempts:
2 left
💡 Hint

Remember how to add custom properties to Error objects.

state_output
expert
2:00remaining
What is the final state of errorMessage after this Next.js server action runs?

Given this server action, what is the value of errorMessage after execution?

NextJS
let errorMessage = ''

export async function action() {
  try {
    await Promise.reject(new Error('Failed to fetch'))
  } catch (error) {
    errorMessage = error.message
  }
  return errorMessage
}
A"Failed to fetch"
B"" (empty string)
Cundefined
DPromise rejected error
Attempts:
2 left
💡 Hint

Check how the catch block sets errorMessage.