Complete the code to log an error message in Next.js using console.
console.[1]('An error occurred while fetching data');
Use console.error to log error messages clearly in the console.
Complete the code to send error details to an external logging service in Next.js.
await fetch('/api/log-error', { method: 'POST', body: JSON.stringify({ error: [1] }) });
Use error.message to send the error description to the logging service.
Fix the error in the code to catch and log errors properly in Next.js API route.
export default async function handler(req, res) {
try {
// some code
} catch ([1]) {
console.error(error);
res.status(500).json({ message: 'Server error' });
}
}The catch block variable must match the variable used inside the block. Here, error is used, so the catch parameter should be error.
Fill both blanks to create a custom error logging function that logs and sends errors to an API.
async function logError([1]) { console.error([2]); await fetch('/api/log-error', { method: 'POST', body: JSON.stringify({ error: [2].message }) }); }
The function parameter and the variable used inside must be the same. Here, error is used consistently.
Fill all three blanks to create a Next.js API route that logs errors and returns a JSON response.
export default async function handler(req, res) {
try {
// process request
} catch ([1]) {
console.error([2]);
res.status([3]).json({ error: [2].message });
}
}The catch parameter and console.error argument must be the same variable error. The HTTP status code for server errors is 500.