0
0
Rest APIprogramming~10 mins

500 Internal Server Error in Rest API - Interactive Code Practice

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

Complete the code to return a 500 Internal Server Error response in a REST API.

Rest API
return [1](500, 'Internal Server Error')
Drag options to blanks, or click blank then click option'
Asend
Breturn
Craise
Dabort
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'return' without specifying the status code.
Using 'raise' without an exception.
Using 'send' which is not a valid function here.
2fill in blank
medium

Complete the code to catch an exception and return a 500 error in a REST API.

Rest API
try:
    process_request()
except Exception as [1]:
    return {'error': 'Internal Server Error'}, 500
Drag options to blanks, or click blank then click option'
Aex
Berr
Ce
Derror
Attempts:
3 left
💡 Hint
Common Mistakes
Leaving the exception variable blank.
Using a variable name that is not valid Python identifier.
3fill in blank
hard

Fix the error in the code to correctly return a 500 Internal Server Error with JSON response.

Rest API
from flask import jsonify

@app.errorhandler([1])
def handle_500(error):
    response = jsonify({'message': 'Internal Server Error'})
    response.status_code = 500
    return response
Drag options to blanks, or click blank then click option'
A500
B400
C200
D404
Attempts:
3 left
💡 Hint
Common Mistakes
Using 404 or 400 which are different error codes.
Using 200 which means success.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps error codes to messages including 500 Internal Server Error.

Rest API
error_messages = {code: [1] for code in [400, 404, [2]]}
Drag options to blanks, or click blank then click option'
A'Error occurred'
B500
C'Internal Server Error'
D'Not Found'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a code that is not an integer.
Using a message that does not describe an error.
5fill in blank
hard

Fill all three blanks to create a function that logs an error and returns a 500 Internal Server Error response.

Rest API
def handle_error(error):
    logger.[1](f'Error: [2]')
    return [3](500)
Drag options to blanks, or click blank then click option'
Aerror
Cabort
Derror_message
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong logging method like 'info' or 'debug'.
Returning without specifying the 500 status code.