Merge remote-tracking branch 'origin/better-error'
This commit is contained in:
commit
96a97f667c
6 changed files with 233 additions and 120 deletions
130
endpoints/api.py
130
endpoints/api.py
|
@ -55,6 +55,38 @@ def csrf_protect():
|
|||
req_user)
|
||||
|
||||
|
||||
@api.errorhandler(404)
|
||||
def endpoint_not_found(e):
|
||||
return jsonify({
|
||||
'error_code': 404,
|
||||
'message': 'Resource not found'
|
||||
})
|
||||
|
||||
|
||||
@api.errorhandler(403)
|
||||
def endpoint_forbidden(e):
|
||||
return jsonify({
|
||||
'error_code': 403,
|
||||
'message': 'Permission Denied'
|
||||
})
|
||||
|
||||
|
||||
@api.errorhandler(400)
|
||||
def endpoint_invalid_request(e):
|
||||
return jsonify({
|
||||
'error_code': 400,
|
||||
'message': 'Invalid Request'
|
||||
})
|
||||
|
||||
|
||||
def request_error(exception=None, **kwargs):
|
||||
data = kwargs.copy()
|
||||
if exception:
|
||||
data['message'] = exception.message
|
||||
|
||||
return make_response(jsonify(data), 400)
|
||||
|
||||
|
||||
def get_route_data():
|
||||
global route_data
|
||||
if route_data:
|
||||
|
@ -139,7 +171,7 @@ def discovery():
|
|||
@api.route('/')
|
||||
@internal_api_call
|
||||
def welcome():
|
||||
return make_response('welcome', 200)
|
||||
return jsonify({'version': '0.5'})
|
||||
|
||||
|
||||
@api.route('/plans/')
|
||||
|
@ -229,20 +261,14 @@ def convert_user_to_organization():
|
|||
# Ensure that the new admin user is the not user being converted.
|
||||
admin_username = convert_data['adminUser']
|
||||
if admin_username == user.username:
|
||||
error_resp = jsonify({
|
||||
'reason': 'invaliduser'
|
||||
})
|
||||
error_resp.status_code = 400
|
||||
return error_resp
|
||||
return request_error(reason='invaliduser',
|
||||
message='The admin user is not valid')
|
||||
|
||||
# Ensure that the sign in credentials work.
|
||||
admin_password = convert_data['adminPassword']
|
||||
if not model.verify_user(admin_username, admin_password):
|
||||
error_resp = jsonify({
|
||||
'reason': 'invaliduser'
|
||||
})
|
||||
error_resp.status_code = 400
|
||||
return error_resp
|
||||
return request_error(reason='invaliduser',
|
||||
message='The admin user credentials are not valid')
|
||||
|
||||
# Subscribe the organization to the new plan.
|
||||
plan = convert_data['plan']
|
||||
|
@ -278,22 +304,15 @@ def change_user_details():
|
|||
new_email = user_data['email']
|
||||
if model.find_user_by_email(new_email):
|
||||
# Email already used.
|
||||
error_resp = jsonify({
|
||||
'message': 'E-mail address already used'
|
||||
})
|
||||
error_resp.status_code = 400
|
||||
return error_resp
|
||||
return request_error(message='E-mail address already used')
|
||||
|
||||
logger.debug('Sending email to change email address for user: %s', user.username)
|
||||
logger.debug('Sending email to change email address for user: %s',
|
||||
user.username)
|
||||
code = model.create_confirm_email_code(user, new_email=new_email)
|
||||
send_change_email(user.username, user_data['email'], code.code)
|
||||
|
||||
except model.InvalidPasswordException, ex:
|
||||
error_resp = jsonify({
|
||||
'message': ex.message,
|
||||
})
|
||||
error_resp.status_code = 400
|
||||
return error_resp
|
||||
return request_error(exception=ex)
|
||||
|
||||
return jsonify(user_view(user))
|
||||
|
||||
|
@ -305,11 +324,7 @@ def create_new_user():
|
|||
|
||||
existing_user = model.get_user(user_data['username'])
|
||||
if existing_user:
|
||||
error_resp = jsonify({
|
||||
'message': 'The username already exists'
|
||||
})
|
||||
error_resp.status_code = 400
|
||||
return error_resp
|
||||
return request_error(message='The username already exists')
|
||||
|
||||
try:
|
||||
new_user = model.create_user(user_data['username'], user_data['password'],
|
||||
|
@ -318,11 +333,7 @@ def create_new_user():
|
|||
send_confirmation_email(new_user.username, new_user.email, code.code)
|
||||
return make_response('Created', 201)
|
||||
except model.DataModelException as ex:
|
||||
error_resp = jsonify({
|
||||
'message': ex.message,
|
||||
})
|
||||
error_resp.status_code = 400
|
||||
return error_resp
|
||||
return request_error(exception=ex)
|
||||
|
||||
|
||||
@api.route('/signin', methods=['POST'])
|
||||
|
@ -343,7 +354,7 @@ def conduct_signin(username_or_email, password):
|
|||
verified = model.verify_user(username_or_email, password)
|
||||
if verified:
|
||||
if common_login(verified):
|
||||
return make_response('Success', 200)
|
||||
return jsonify({'success': True})
|
||||
else:
|
||||
needs_email_verification = True
|
||||
|
||||
|
@ -364,7 +375,7 @@ def conduct_signin(username_or_email, password):
|
|||
def logout():
|
||||
logout_user()
|
||||
identity_changed.send(app, identity=AnonymousIdentity())
|
||||
return make_response('Success', 200)
|
||||
return jsonify({'success': True})
|
||||
|
||||
|
||||
@api.route("/recovery", methods=['POST'])
|
||||
|
@ -466,22 +477,15 @@ def create_organization():
|
|||
pass
|
||||
|
||||
if existing:
|
||||
error_resp = jsonify({
|
||||
'message': 'A user or organization with this name already exists'
|
||||
})
|
||||
error_resp.status_code = 400
|
||||
return error_resp
|
||||
msg = 'A user or organization with this name already exists'
|
||||
return request_error(message=msg)
|
||||
|
||||
try:
|
||||
model.create_organization(org_data['name'], org_data['email'],
|
||||
current_user.db_user())
|
||||
return make_response('Created', 201)
|
||||
except model.DataModelException as ex:
|
||||
error_resp = jsonify({
|
||||
'message': ex.message,
|
||||
})
|
||||
error_resp.status_code = 400
|
||||
return error_resp
|
||||
return request_error(exception=ex)
|
||||
|
||||
|
||||
def org_view(o, teams):
|
||||
|
@ -536,12 +540,7 @@ def change_organization_details(orgname):
|
|||
if 'email' in org_data and org_data['email'] != org.email:
|
||||
new_email = org_data['email']
|
||||
if model.find_user_by_email(new_email):
|
||||
# Email already used.
|
||||
error_resp = jsonify({
|
||||
'message': 'E-mail address already used'
|
||||
})
|
||||
error_resp.status_code = 400
|
||||
return error_resp
|
||||
return request_error(message='E-mail address already used')
|
||||
|
||||
logger.debug('Changing email address for organization: %s', org.username)
|
||||
model.update_email(org, new_email)
|
||||
|
@ -626,7 +625,8 @@ def create_organization_prototype_permission(orgname):
|
|||
details = request.get_json()
|
||||
activating_username = None
|
||||
|
||||
if 'activating_user' in details and details['activating_user'] and 'name' in details['activating_user']:
|
||||
if ('activating_user' in details and details['activating_user'] and
|
||||
'name' in details['activating_user']):
|
||||
activating_username = details['activating_user']['name']
|
||||
|
||||
delegate = details['delegate']
|
||||
|
@ -644,10 +644,10 @@ def create_organization_prototype_permission(orgname):
|
|||
if delegate_teamname else None)
|
||||
|
||||
if activating_username and not activating_user:
|
||||
abort(404)
|
||||
return request_error(message='Unknown activating user')
|
||||
|
||||
if not delegate_user and not delegate_team:
|
||||
abort(400)
|
||||
return request_error(message='Missing delagate user or team')
|
||||
|
||||
role_name = details['role']
|
||||
|
||||
|
@ -905,7 +905,7 @@ def update_organization_team_member(orgname, teamname, membername):
|
|||
# Find the user.
|
||||
user = model.get_user(membername)
|
||||
if not user:
|
||||
abort(400)
|
||||
return request_error(message='Unknown user')
|
||||
|
||||
# Add the user to the team.
|
||||
model.add_user_to_team(user, team)
|
||||
|
@ -946,7 +946,7 @@ def create_repo():
|
|||
|
||||
existing = model.get_repository(namespace_name, repository_name)
|
||||
if existing:
|
||||
return make_response('Repository already exists', 400)
|
||||
return request_error(message='Repository already exists')
|
||||
|
||||
visibility = req['visibility']
|
||||
|
||||
|
@ -1019,7 +1019,7 @@ def list_repos():
|
|||
if page:
|
||||
try:
|
||||
page = int(page)
|
||||
except:
|
||||
except Exception:
|
||||
page = None
|
||||
|
||||
username = None
|
||||
|
@ -1550,11 +1550,7 @@ def change_user_permissions(namespace, repository, username):
|
|||
# This repository is not part of an organization
|
||||
pass
|
||||
except model.DataModelException as ex:
|
||||
error_resp = jsonify({
|
||||
'message': ex.message,
|
||||
})
|
||||
error_resp.status_code = 400
|
||||
return error_resp
|
||||
return request_error(exception=ex)
|
||||
|
||||
log_action('change_repo_permission', namespace,
|
||||
{'username': username, 'repo': repository,
|
||||
|
@ -1607,11 +1603,7 @@ def delete_user_permissions(namespace, repository, username):
|
|||
try:
|
||||
model.delete_user_permission(username, namespace, repository)
|
||||
except model.DataModelException as ex:
|
||||
error_resp = jsonify({
|
||||
'message': ex.message,
|
||||
})
|
||||
error_resp.status_code = 400
|
||||
return error_resp
|
||||
return request_error(exception=ex)
|
||||
|
||||
log_action('delete_repo_permission', namespace,
|
||||
{'username': username, 'repo': repository},
|
||||
|
@ -1867,7 +1859,7 @@ def subscribe(user, plan, token, require_business_plan):
|
|||
plan_found['price'] == 0):
|
||||
logger.warning('Business attempting to subscribe to personal plan: %s',
|
||||
user.username)
|
||||
abort(400)
|
||||
return request_error(message='No matching plan found')
|
||||
|
||||
private_repos = model.get_private_repo_count(user.username)
|
||||
|
||||
|
@ -2097,7 +2089,7 @@ def delete_user_robot(robot_shortname):
|
|||
parent = current_user.db_user()
|
||||
model.delete_robot(format_robot_username(parent.username, robot_shortname))
|
||||
log_action('delete_robot', parent.username, {'robot': robot_shortname})
|
||||
return make_response('No Content', 204)
|
||||
return make_response('Deleted', 204)
|
||||
|
||||
|
||||
@api.route('/organization/<orgname>/robots/<robot_shortname>',
|
||||
|
@ -2109,7 +2101,7 @@ def delete_org_robot(orgname, robot_shortname):
|
|||
if permission.can():
|
||||
model.delete_robot(format_robot_username(orgname, robot_shortname))
|
||||
log_action('delete_robot', orgname, {'robot': robot_shortname})
|
||||
return make_response('No Content', 204)
|
||||
return make_response('Deleted', 204)
|
||||
|
||||
abort(403)
|
||||
|
||||
|
|
Reference in a new issue