0
0
Flaskframework~10 mins

Why error handling matters in Flask - Test Your Understanding

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

Complete the code to import the Flask class.

Flask
from flask import [1]
app = [1](__name__)
Drag options to blanks, or click blank then click option'
Ajsonify
BFlask
Crender_template
DRequest
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request instead of Flask
Using jsonify as the app class
2fill in blank
medium

Complete the code to define a route that returns a simple message.

Flask
@app.route('/')
def home():
    return [1]
Drag options to blanks, or click blank then click option'
AHello World!
Bjsonify(message='Hello World!')
Crender_template('index.html')
D'Hello World!'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning unquoted text
Returning a function call instead of string
3fill in blank
hard

Fix the error in the code to handle a 404 error with a custom message.

Flask
@app.errorhandler([1])
def not_found(error):
    return 'Page not found', 404
Drag options to blanks, or click blank then click option'
A404
B403
C500
D400
Attempts:
3 left
💡 Hint
Common Mistakes
Using 500 for server error instead of 404
Using 403 for forbidden
4fill in blank
hard

Fill in the blank to create a route that raises a 404 error manually.

Flask
from flask import abort

@app.route('/secret')
def secret():
    [1](404)
    return 'This is a secret page.'
Drag options to blanks, or click blank then click option'
Aabort
Breturn
Craise
Dprint
Attempts:
3 left
💡 Hint
Common Mistakes
Using raise instead of abort
Using return to send error code
5fill in blank
hard

Fill all three blanks to handle errors and return JSON with error info.

Flask
from flask import jsonify

@app.errorhandler([1])
def handle_error(error):
    response = jsonify({'error': str(error)})
    response.status_code = [2]
    return [3]
Drag options to blanks, or click blank then click option'
A404
Bresponse
Cerror.code
D500
Attempts:
3 left
💡 Hint
Common Mistakes
Hardcoding status code incorrectly
Returning error instead of response