Complete worked example
python
from flask import Flask, abort
app = Flask(__name__)
app.config['TESTING'] = True
@app.errorhandler(404)
def not_found(error):
return {'error': 'not found'}, 404
@app.get('/roles/<int:role_id>')
def detail(role_id):
if role_id != 1:
abort(404)
return {'id': 1}
def response_for(path):
response = app.test_client().get(path)
return {'status': response.status_code, 'body': response.json,
'mime': response.mimetype}- Reasoning
- The route exposes one fictional role and aborts with 404 for any other identifier. A dedicated handler produces a small JSON error response for both an absent object and an unmatched path.