Add an exact abort and use it to send the expected response for 409s.
This commit is contained in:
parent
78c5aec5b9
commit
7a5605a568
2 changed files with 28 additions and 19 deletions
34
util/http.py
34
util/http.py
|
@ -16,6 +16,28 @@ DEFAULT_MESSAGE[404] = 'Not Found'
|
|||
DEFAULT_MESSAGE[409] = 'Conflict'
|
||||
DEFAULT_MESSAGE[501] = 'Not Implemented'
|
||||
|
||||
|
||||
def _abort(status_code, data_object, headers):
|
||||
# Add CORS headers to all errors
|
||||
options_resp = current_app.make_default_options_response()
|
||||
headers['Access-Control-Allow-Origin'] = '*'
|
||||
headers['Access-Control-Allow-Methods'] = options_resp.headers['allow']
|
||||
headers['Access-Control-Max-Age'] = str(21600)
|
||||
headers['Access-Control-Allow-Headers'] = ['Authorization', 'Content-Type']
|
||||
|
||||
resp = make_response(json.dumps(data_object), status_code, headers)
|
||||
|
||||
# Report the abort to the user.
|
||||
flask_abort(resp)
|
||||
|
||||
def exact_abort(status_code, message=None):
|
||||
data = {}
|
||||
|
||||
if message is not None:
|
||||
data['error'] = message
|
||||
|
||||
_abort(status_code, data, {})
|
||||
|
||||
def abort(status_code, message=None, issue=None, headers=None, **kwargs):
|
||||
|
||||
message = (str(message) % kwargs if message else
|
||||
|
@ -57,14 +79,4 @@ def abort(status_code, message=None, issue=None, headers=None, **kwargs):
|
|||
if headers is None:
|
||||
headers = {}
|
||||
|
||||
# Add CORS headers to all errors
|
||||
options_resp = current_app.make_default_options_response()
|
||||
headers['Access-Control-Allow-Origin'] = '*'
|
||||
headers['Access-Control-Allow-Methods'] = options_resp.headers['allow']
|
||||
headers['Access-Control-Max-Age'] = str(21600)
|
||||
headers['Access-Control-Allow-Headers'] = ['Authorization', 'Content-Type']
|
||||
|
||||
resp = make_response(json.dumps(data), status_code, headers)
|
||||
|
||||
# Report the abort to the user.
|
||||
flask_abort(resp)
|
||||
_abort(status_code, data, headers)
|
||||
|
|
Reference in a new issue