0
0
Node.jsframework~10 mins

Why debugging skills matter in Node.js - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print an error message when a promise is rejected.

Node.js
fetch('https://api.example.com/data')
  .then(response => response.json())
  .catch(error => console.[1](error))
Drag options to blanks, or click blank then click option'
Alog
Binfo
Cwarn
Derror
Attempts:
3 left
💡 Hint
Common Mistakes
Using console.log instead of console.error
Using console.warn which is less clear for errors
2fill in blank
medium

Complete the code to catch and log errors in an async function.

Node.js
async function getData() {
  try {
    const response = await fetch('https://api.example.com/data');
    const data = await response.json();
    return data;
  } catch ([1]) {
    console.error(error);
  }
}
Drag options to blanks, or click blank then click option'
Aerr
Be
Cerror
Dexception
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name than the one logged
Leaving the catch parameter empty
3fill in blank
hard

Fix the error in the code to properly handle rejected promises.

Node.js
fetch('https://api.example.com/data')
  .then(response => response.json())
  .then(data => console.log(data))
  .[1](error => console.error(error))
Drag options to blanks, or click blank then click option'
Aerror
Bcatch
Cfail
Dreject
Attempts:
3 left
💡 Hint
Common Mistakes
Using .error or .fail which are not valid promise methods
Using .reject which is not a method
4fill in blank
hard

Fill both blanks to create a simple error handler function.

Node.js
function handleError([1]) {
  console.[2](error.message);
}
Drag options to blanks, or click blank then click option'
Aerror
Blog
Dwarn
Attempts:
3 left
💡 Hint
Common Mistakes
Using console.log instead of console.error
Mismatching parameter and logged variable names
5fill in blank
hard

Fill all three blanks to log error details conditionally.

Node.js
function logErrorDetails([1]) {
  if (error.[2]) {
    console.[3](`Error: ${error.message}`);
  }
}
Drag options to blanks, or click blank then click option'
Aerror
Bstack
Dlog
Attempts:
3 left
💡 Hint
Common Mistakes
Using console.log instead of console.error
Checking a wrong property instead of 'stack'