0
0
Rest APIprogramming~30 mins

Nested error reporting in Rest API - Mini Project: Build & Apply

Choose your learning style9 modes available
Nested Error Reporting in REST API
📖 Scenario: You are building a REST API that receives user data. Sometimes, the data has errors inside nested parts, like an address inside a user profile. You want to collect all errors, including those inside nested data, and send them back in a clear way.
🎯 Goal: Create a Python dictionary that represents nested error messages for a user profile API. Then, write code to extract and display these nested errors clearly.
📋 What You'll Learn
Create a nested dictionary called errors with specific error messages
Create a variable error_count to count total errors
Use a nested for loop to count all errors inside the nested dictionary
Print the total number of errors found
💡 Why This Matters
🌍 Real World
APIs often receive complex data with nested parts. Reporting all errors clearly helps users fix their input quickly.
💼 Career
Backend developers and API designers need to handle nested error reporting to improve user experience and debugging.
Progress0 / 4 steps
1
Create nested error dictionary
Create a dictionary called errors with these exact entries: 'username': 'Required field', 'email': 'Invalid format', and a nested dictionary 'address' with 'street': 'Missing' and 'zipcode': 'Invalid'.
Rest API
Need a hint?

Use a dictionary with keys 'username', 'email', and 'address'. The 'address' key holds another dictionary.

2
Add error count variable
Create a variable called error_count and set it to 0 to start counting errors.
Rest API
Need a hint?

Just create a variable named error_count and set it to zero.

3
Count all errors including nested
Use a for loop with variables field and error to iterate over errors.items(). Inside the loop, check if error is a dictionary. If yes, use another for loop with subfield and suberror to iterate over error.items() and increment error_count by 1 for each nested error. Otherwise, increment error_count by 1 for the top-level error.
Rest API
Need a hint?

Use isinstance(error, dict) to check if the error is nested. Then loop inside it.

4
Print total error count
Write a print statement to display the text 'Total errors:' followed by the value of error_count.
Rest API
Need a hint?

Use an f-string to print the message and the error_count variable.