0
0
NextJSframework~10 mins

Server-side error handling in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Server-side error handling
Request received
Run server code
Try block executes
Success
Return data
Return error response
Client receives response
The server tries to run code. If it works, it sends data. If an error happens, it catches it and sends an error response.
Execution Sample
NextJS
export async function GET() {
  try {
    const data = await fetchData();
    return new Response(JSON.stringify(data), { status: 200 });
  } catch (error) {
    return new Response('Error occurred', { status: 500 });
  }
}
This code tries to fetch data on a GET request and returns it. If fetching fails, it returns a 500 error response.
Execution Table
StepActionTry Block ResultCatch Block TriggeredResponse Returned
1Request receivedNo result yetNoNo
2Try to fetch dataSuccess: data fetchedNoNo
3Return success responseData returnedNoResponse with status 200 and data
4Request receivedNo result yetNoNo
5Try to fetch dataError thrownYesNo
6Catch block runsNoYesNo
7Return error responseNoYesResponse with status 500 and error message
💡 Execution stops after returning a response to the client.
Variable Tracker
VariableStartAfter Step 2 (Success)After Step 5 (Error)
dataundefinedfetched data objectundefined
errorundefinedundefinedError object
Key Moments - 2 Insights
Why does the catch block run only when an error happens?
Because the catch block runs only if the try block throws an error, as shown in execution_table rows 5-7 where an error triggers the catch.
What response does the server send when data fetch is successful?
It sends a response with status 200 and the data, as shown in execution_table row 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the response status when the catch block runs?
A200
B500
C404
D302
💡 Hint
Check the Response Returned column in rows 6 and 7 where the catch block triggers.
At which step does the try block successfully return data?
AStep 3
BStep 2
CStep 5
DStep 7
💡 Hint
Look at the Try Block Result and Response Returned columns in the execution table.
If fetchData always throws an error, how many times does the catch block run per request?
AZero times
BTwice
COnce
DMultiple times
💡 Hint
Refer to the flow where catch block runs only once per error thrown in the try block.
Concept Snapshot
Server-side error handling in Next.js uses try/catch.
Try block runs server code.
If error occurs, catch block sends error response.
Always return a Response object.
Use status codes like 200 for success, 500 for errors.
Full Transcript
In Next.js server-side code, when a request comes in, the server runs code inside a try block. If the code works without errors, it returns a success response with data and status 200. If an error happens, the catch block catches it and returns an error response with status 500. This ensures the client always gets a clear response. The execution table shows steps from receiving the request, trying to fetch data, and either returning success or catching errors and returning an error response. Variables like data and error change depending on success or failure. Understanding when the catch block runs and what response is sent helps beginners handle errors properly in server code.