0
0
Rest APIprogramming~10 mins

Why consistent errors help developers in Rest API - Test Your Understanding

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

Complete the code to return a 404 error when a resource is not found.

Rest API
if resource is None:
    return [1](404, 'Resource not found')
Drag options to blanks, or click blank then click option'
Areturn
Babort
Cprint
Draise
Attempts:
3 left
💡 Hint
Common Mistakes
Using print instead of abort does not send an HTTP error.
Using raise without an exception class will cause a syntax error.
2fill in blank
medium

Complete the code to return a JSON error message with status 400.

Rest API
return [1]({'error': 'Bad request'}), 400
Drag options to blanks, or click blank then click option'
Ajsonify
Bjson
Cstr
Dprint
Attempts:
3 left
💡 Hint
Common Mistakes
Using json returns a string, not a response object.
Using print does not send a response to the client.
3fill in blank
hard

Fix the error in the code to send a consistent 500 error response.

Rest API
try:
    process_request()
except Exception as e:
    return [1]({'error': str(e)}), 500
Drag options to blanks, or click blank then click option'
Astr
Bprint
Cjsonify
Dabort
Attempts:
3 left
💡 Hint
Common Mistakes
Using print does not send a response to the client.
Using abort without a message does not include error details.
4fill in blank
hard

Fill both blanks to create a consistent error handler for 403 Forbidden errors.

Rest API
@app.errorhandler([1])
def forbidden_error(error):
    return [2]({'error': 'Forbidden'}), 403
Drag options to blanks, or click blank then click option'
A403
Bjsonify
C404
Dabort
Attempts:
3 left
💡 Hint
Common Mistakes
Using 404 instead of 403 changes the error meaning.
Using abort inside the handler can cause recursion.
5fill in blank
hard

Fill all three blanks to create a consistent error response with a custom message and status code.

Rest API
def error_response(message, code):
    response = [1]({'error': message})
    response.status_code = [2]
    return response

return error_response([3], 401)
Drag options to blanks, or click blank then click option'
Ajsonify
B401
C'Unauthorized access'
Dabort
Attempts:
3 left
💡 Hint
Common Mistakes
Setting status_code to a string instead of an integer.
Passing the message without quotes causes a syntax error.