Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using console.log instead of console.error
Using console.warn which is less clear for errors
✗ Incorrect
Use console.error to clearly show error messages in Node.js debugging.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name than the one logged
Leaving the catch parameter empty
✗ Incorrect
The catch block uses error as the variable name to hold the caught error.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using .error or .fail which are not valid promise methods
Using .reject which is not a method
✗ Incorrect
The correct method to handle rejected promises is catch.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using console.log instead of console.error
Mismatching parameter and logged variable names
✗ Incorrect
The function takes error as parameter and logs the message using console.error.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using console.log instead of console.error
Checking a wrong property instead of 'stack'
✗ Incorrect
The function checks if the error has a stack trace and logs the message using console.error.