Merge pull request #2518 from jakedt/fixflaskrestful
Fix for flask-restful >0.3 error handling
This commit is contained in:
commit
98da5d6a25
1 changed files with 16 additions and 10 deletions
|
@ -30,22 +30,28 @@ from util.pagination import encrypt_page_token, decrypt_page_token
|
|||
|
||||
logger = logging.getLogger(__name__)
|
||||
api_bp = Blueprint('api', __name__)
|
||||
api = Api()
|
||||
|
||||
|
||||
class ApiExceptionHandlingApi(Api):
|
||||
@crossdomain(origin='*', headers=['Authorization', 'Content-Type'])
|
||||
def handle_error(self, error):
|
||||
if isinstance(error, ApiException):
|
||||
response = Response(json.dumps(error.to_dict()), error.status_code,
|
||||
mimetype='application/json')
|
||||
if error.status_code == 401:
|
||||
response.headers['WWW-Authenticate'] = ('Bearer error="%s" error_description="%s"' %
|
||||
(error.error_type.value, error.error_description))
|
||||
return response
|
||||
return super(ApiExceptionHandlingApi, self).handle_error(error)
|
||||
|
||||
|
||||
api = ApiExceptionHandlingApi()
|
||||
api.init_app(api_bp)
|
||||
api.decorators = [csrf_protect(),
|
||||
crossdomain(origin='*', headers=['Authorization', 'Content-Type']),
|
||||
process_oauth, time_decorator(api_bp.name, metric_queue)]
|
||||
|
||||
|
||||
@api_bp.app_errorhandler(ApiException)
|
||||
@crossdomain(origin='*', headers=['Authorization', 'Content-Type'])
|
||||
def handle_api_error(error):
|
||||
response = Response(json.dumps(error.to_dict()), error.status_code, mimetype='application/json')
|
||||
if error.status_code == 401:
|
||||
response.headers['WWW-Authenticate'] = ('Bearer error="%s" error_description="%s"' %
|
||||
(error.error_type.value, error.error_description))
|
||||
return response
|
||||
|
||||
def resource(*urls, **kwargs):
|
||||
def wrapper(api_resource):
|
||||
if not api_resource:
|
||||
|
|
Reference in a new issue