0
0
Node.jsframework~10 mins

Async/await error handling patterns in Node.js - Interactive Code Practice

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

Complete the code to catch errors using try/catch with async/await.

Node.js
async function fetchData() {
  try {
    const data = await fetch('[1]');
    return data.json();
  } catch (error) {
    console.error('Error:', error);
  }
}
Drag options to blanks, or click blank then click option'
A'https://api.example.com/data'
Bfetch('https://api.example.com/data')
Cawait fetch('https://api.example.com/data')
D'data'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the fetch call itself instead of the URL string.
Forgetting to put the URL inside quotes.
2fill in blank
medium

Complete the code to rethrow an error after logging it inside a catch block.

Node.js
async function getUser() {
  try {
    const response = await fetch('/user');
    return await response.json();
  } catch (error) {
    console.error('Fetch failed:', error);
    [1];
  }
}
Drag options to blanks, or click blank then click option'
Areturn error
Bthrow new Error()
Cthrow error
Dconsole.log(error)
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the error instead of throwing it.
Throwing a new empty error losing original info.
3fill in blank
hard

Fix the error in this async function by adding proper error handling.

Node.js
async function loadData() {
  const response = await fetch('/data');
  const json = await response.json();
  return json;
  [1]
}
Drag options to blanks, or click blank then click option'
Atry { const response = await fetch('/data'); const json = await response.json(); return json; } catch (error) { console.error(error); }
Btry { } catch (error) { console.error(error); }
Cfinally { console.log('Done'); }
Dcatch (error) { console.error(error); }
Attempts:
3 left
💡 Hint
Common Mistakes
Adding catch block outside function body.
Using finally without try/catch.
Not wrapping await calls in try/catch.
4fill in blank
hard

Fill both blanks to create an async function that returns null on error instead of throwing.

Node.js
async function safeFetch() {
  try {
    const response = await fetch('[1]');
    return await response.json();
  } catch ([2]) {
    return null;
  }
}
Drag options to blanks, or click blank then click option'
A'/safe-data'
Berr
Cerror
D'/data'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong URL string.
Missing error variable in catch.
5fill in blank
hard

Fill all three blanks to create an async function that logs error message and returns fallback data.

Node.js
async function fetchWithFallback() {
  try {
    const res = await fetch('[1]');
    if (!res.ok) throw new Error(res.statusText);
    return await res.json();
  } catch ([2]) {
    console.error('Fetch error:', [3].message);
    return { data: [] };
  }
}
Drag options to blanks, or click blank then click option'
A'/api/items'
Berr
Cerror
De
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in catch and console.error.
Not checking response status before parsing JSON.