0
0
Rest APIprogramming~20 mins

Graceful degradation in Rest API - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Graceful Degradation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this REST API error handling code?

Consider a REST API endpoint that tries to fetch user data. If the database is down, it returns a fallback message. What will be the JSON response when the database is unreachable?

Rest API
from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/user/<int:user_id>')
def get_user(user_id):
    try:
        # Simulate database call
        raise ConnectionError('DB down')
    except ConnectionError:
        return jsonify({'message': 'Service temporarily unavailable, please try later'}), 503

if __name__ == '__main__':
    app.run()
A{"message": "Service temporarily unavailable, please try later"}
B{"error": "Database connection failed"}
C{"user_id": 1, "name": "John Doe"}
D500 Internal Server Error
Attempts:
2 left
💡 Hint

Look at the except block and what JSON it returns.

🧠 Conceptual
intermediate
1:30remaining
Which best describes graceful degradation in REST APIs?

Choose the statement that best explains the concept of graceful degradation in REST APIs.

AThe API provides a simpler or fallback response when some components fail.
BThe API stops working completely when a service fails.
CThe API ignores errors and returns empty responses.
DThe API crashes and returns a 500 error for all requests.
Attempts:
2 left
💡 Hint

Think about how the API behaves when something goes wrong but still tries to respond usefully.

🔧 Debug
advanced
2:30remaining
Why does this graceful degradation code fail to return fallback data?

Examine the following Flask REST API code. It is supposed to return fallback data if the main service fails. Why does it not return the fallback data?

Rest API
from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/data')
def get_data():
    try:
        # Simulate failure
        raise Exception('Service error')
    except Exception:
        fallback = {'data': 'default'}
    return jsonify(fallback)

if __name__ == '__main__':
    app.run()
AThe try block should return fallback data directly, not in except.
BThe variable 'fallback' is not defined if no exception occurs, causing a NameError.
CThe jsonify function is called outside the except block, causing a syntax error.
DThe except block does not return a response, so the function returns None.
Attempts:
2 left
💡 Hint

What happens if the try block does not raise an exception?

📝 Syntax
advanced
1:30remaining
Which option fixes the syntax error in this graceful degradation snippet?

Identify the option that fixes the syntax error in this Python REST API snippet using graceful degradation.

Rest API
def fetch_data():
    try
        data = get_remote_data()
    except Exception:
        data = {'message': 'Fallback data'}
    return data
AIndent the except block at the same level as try.
BRemove the except block entirely.
CChange try to try():
DAdd a colon after try: <br> def fetch_data():<br> &nbsp;&nbsp;try:<br> &nbsp;&nbsp;&nbsp;&nbsp;data = get_remote_data()<br> &nbsp;&nbsp;except Exception:<br> &nbsp;&nbsp;&nbsp;&nbsp;data = {'message': 'Fallback data'}<br> &nbsp;&nbsp;return data
Attempts:
2 left
💡 Hint

Check the syntax for try-except blocks in Python.

🚀 Application
expert
2:30remaining
How many fallback responses does this REST API provide?

Given the following Flask REST API code, how many different fallback JSON responses can the API return when the main service fails?

Rest API
from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/info')
def info():
    try:
        # Simulate service failure
        raise ValueError('Service error')
    except ValueError:
        return jsonify({'error': 'Service temporarily unavailable'}), 503
    except Exception:
        return jsonify({'error': 'Unknown error occurred'}), 500

if __name__ == '__main__':
    app.run()
A0
B2
C1
D3
Attempts:
2 left
💡 Hint

Which except block will run given the raised exception?