Bird
Raised Fist0
NextJSframework~20 mins

Error logging strategies in NextJS - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Error Logging Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding centralized error logging

Which of the following best describes the main benefit of using centralized error logging in a Next.js application?

AIt stores error logs only on the user's browser for privacy reasons.
BIt automatically fixes errors in the code without developer intervention.
CIt prevents errors from occurring by validating user input on the client side only.
DIt collects all error logs in one place, making it easier to monitor and analyze issues across multiple servers or environments.
Attempts:
2 left
💡 Hint

Think about how error logs from different parts of your app can be managed efficiently.

💻 Command Output
intermediate
2:00remaining
Next.js error logging output with Sentry

What will be the output in the Sentry dashboard after running this Next.js server-side code snippet that throws an error?

NextJS
import * as Sentry from '@sentry/nextjs';

export async function getServerSideProps() {
  try {
    throw new Error('Database connection failed');
  } catch (error) {
    Sentry.captureException(error);
    return { props: { error: error.message } };
  }
}
AThe error message is logged to the browser console only, not to Sentry.
BNo error is logged because captureException is asynchronous and not awaited.
CAn error event with message 'Database connection failed' appears in Sentry with stack trace.
DThe function returns a syntax error due to missing await keyword.
Attempts:
2 left
💡 Hint

Consider how Sentry captures exceptions even if not awaited.

Configuration
advanced
2:00remaining
Configuring log levels in Next.js with Winston

Given the following Winston logger configuration in a Next.js API route, which log messages will be saved to the file 'error.log'?

NextJS
import winston from 'winston';

const logger = winston.createLogger({
  level: 'warn',
  transports: [
    new winston.transports.File({ filename: 'error.log' })
  ]
});

logger.error('Critical failure');
logger.warn('Warning message');
logger.info('Informational message');
A'Critical failure' and 'Warning message' will be saved; 'Informational message' will be ignored.
BOnly 'Critical failure' will be saved; others ignored.
CAll three messages will be saved to 'error.log'.
DNo messages will be saved because the level is set incorrectly.
Attempts:
2 left
💡 Hint

Remember that log levels filter messages at or above the set level.

Troubleshoot
advanced
2:00remaining
Diagnosing missing error logs in production

A Next.js app uses a custom error logging middleware that writes errors to a remote logging service. In production, no error logs appear in the service. Which is the most likely cause?

AThe remote logging service is down, but the app logs errors locally instead.
BThe middleware is only applied in development mode and not included in the production build.
CThe app is catching errors but not throwing any exceptions.
DThe logging middleware is configured to log only warnings, but errors are not warnings.
Attempts:
2 left
💡 Hint

Check if the middleware is active in all environments.

🔀 Workflow
expert
3:00remaining
Implementing a robust error logging workflow in Next.js

Which sequence of steps correctly describes a robust error logging workflow for a Next.js app that uses Sentry and Winston?

A1, 2, 3, 4
B1, 3, 2, 4
C2, 1, 3, 4
D3, 1, 2, 4
Attempts:
2 left
💡 Hint

Think about capturing errors first, then reporting and logging, finally responding to the user.

Practice

(1/5)
1. What is the main purpose of error logging in a Next.js application?
easy
A. To speed up the app loading time
B. To reduce the size of the app bundle
C. To increase the app's SEO ranking
D. To catch and record errors for fixing issues later

Solution

  1. Step 1: Understand error logging purpose

    Error logging is used to catch errors that happen during app execution.
  2. Step 2: Identify why errors are logged

    Logging errors helps developers find and fix problems to improve app stability.
  3. Final Answer:

    To catch and record errors for fixing issues later -> Option D
  4. Quick Check:

    Error logging purpose = Catch and fix errors [OK]
Hint: Error logging is for catching and fixing problems [OK]
Common Mistakes:
  • Confusing error logging with performance optimization
  • Thinking error logging improves SEO
  • Believing error logging reduces app size
2. Which of the following is the correct syntax to catch and log an error in Next.js using try...catch?
easy
A. try { /* code */ } catch { console.error }
B. try { /* code */ } catch (error) { console.error(error) }
C. try (/* code */) catch (error) { console.error(error) }
D. try { /* code */ } catch error { console.log(error) }

Solution

  1. Step 1: Review correct try...catch syntax

    The correct syntax uses parentheses around the error variable in catch: catch (error).
  2. Step 2: Check logging method

    console.error(error) correctly logs the error object to the console.
  3. Final Answer:

    try { /* code */ } catch (error) { console.error(error) } -> Option B
  4. Quick Check:

    Correct try...catch syntax = try { /* code */ } catch (error) { console.error(error) } [OK]
Hint: Catch uses parentheses and console.error logs errors [OK]
Common Mistakes:
  • Omitting parentheses in catch
  • Using console.log instead of console.error
  • Incorrect try or catch block syntax
3. What will be the console output when this Next.js code runs and an error occurs?
try {
  throw new Error('Failed to fetch data')
} catch (err) {
  console.error('Error:', err.message)
}
medium
A. Error: Failed to fetch data
B. Failed to fetch data
C. Error: Error: Failed to fetch data
D. undefined

Solution

  1. Step 1: Understand the thrown error

    The code throws an Error object with message 'Failed to fetch data'.
  2. Step 2: Analyze the catch block output

    console.error prints 'Error:' plus the error message, resulting in 'Error: Failed to fetch data'.
  3. Final Answer:

    Error: Failed to fetch data -> Option A
  4. Quick Check:

    console.error prints label + error message = Error: Failed to fetch data [OK]
Hint: console.error prints label plus error.message string [OK]
Common Mistakes:
  • Confusing error object with error message string
  • Expecting double 'Error:' prefix
  • Ignoring the label string in console.error
4. Identify the error in this Next.js error logging code:
try {
  const data = await fetchData()
} catch {
  console.error(error)
}
medium
A. Missing error parameter in catch block
B. Using console.error instead of console.log
C. fetchData() should not be awaited
D. try block should not have await

Solution

  1. Step 1: Check catch block syntax

    The catch block is missing the error parameter, so 'error' is undefined inside it.
  2. Step 2: Understand error usage

    console.error(error) fails because 'error' is not declared; catch must declare it like catch (error).
  3. Final Answer:

    Missing error parameter in catch block -> Option A
  4. Quick Check:

    Catch needs error parameter to log it [OK]
Hint: Always name error in catch to use it inside [OK]
Common Mistakes:
  • Omitting error parameter in catch
  • Thinking console.error is wrong here
  • Misunderstanding async/await usage
5. In a production Next.js app, which strategy is best for error logging to ensure issues are tracked and fixed efficiently?
hard
A. Disable all error logging to improve performance
B. Only use console.error locally and ignore errors in production
C. Send errors to an external monitoring service and log locally
D. Log errors only in the browser console without server tracking

Solution

  1. Step 1: Understand production error logging needs

    In production, errors should be tracked centrally to fix issues quickly.
  2. Step 2: Identify best practice

    Sending errors to external services (like Sentry) plus local logging helps monitor and debug effectively.
  3. Final Answer:

    Send errors to an external monitoring service and log locally -> Option C
  4. Quick Check:

    Production error tracking = external service + local logs [OK]
Hint: Use external services plus local logs in production [OK]
Common Mistakes:
  • Ignoring production error tracking
  • Disabling logging to save performance
  • Logging only in browser without server monitoring