Complete the code to catch errors in a server action.
export async function createUser(data) {
try {
// code to create user
} catch (error) {
[1];
}
}Using console.error(error) logs the error for debugging without stopping the server action.
Complete the code to throw a custom error message in a server action.
export async function deleteUser(id) {
if (!id) {
[1];
}
// deletion logic
}Throwing a new Error with a message stops execution and signals the problem clearly.
Fix the error in the server action to properly handle rejected promises.
export async function updateUser(data) {
try {
await updateDatabase(data).[1];
} catch (error) {
throw error;
}
}Using catch() after a promise handles any rejection properly.
Fill both blanks to return a JSON error response with status code 400.
export async function serverAction(req) {
try {
// process request
} catch (error) {
return new Response(JSON.stringify({ error: error.message }), { status: [1], headers: { 'Content-Type': [2] } });
}
}Status code 400 means bad request. The content type must be 'application/json' for JSON data.
Fill all three blanks to log the error, set a 500 status, and return a JSON error message.
export async function handleRequest(req) {
try {
// handle request
} catch (error) {
[1];
return new Response(JSON.stringify({ message: [2] }), { status: [3] });
}
}Logging the error helps debugging. Returning status 500 signals server error with a clear message.