Complete the code to import the error handling function from Next.js.
import { [1] } from 'next/server';
The NextResponse object is imported from 'next/server' to handle responses including errors on the server side.
Complete the code to return a 404 error with a message in a Next.js server function.
return new [1]('Not Found', { status: 404 });
Returning a new Response with a status code is the standard way to handle server-side errors in Next.js. NextResponse is used for additional Next.js features but does not support this constructor signature.
Fix the error in this server function to correctly catch and handle errors.
export async function GET() {
try {
const data = await fetchData();
return NextResponse.json(data);
} catch (error) {
return new [1]('Server Error', { status: 500 });
}
}To return an error response in Next.js server functions, you create a new Response with the error message and status code. NextResponse does not support this constructor signature.
Fill both blanks to create a JSON error response with status 400 in a Next.js server function.
return NextResponse.[1]({ error: 'Bad Request' }, { status: [2] });
The NextResponse.json method sends a JSON response, and the status code 400 indicates a bad request error.
Fill all three blanks to handle a missing parameter error and return a JSON response with status 422.
if (!params.id) { return NextResponse.[1]({ message: 'ID is required' }, { status: [2] }); } throw new [3]('Unexpected error');
Use NextResponse.json to send JSON data, status 422 means unprocessable entity, and Error is thrown for unexpected errors.