Merge branch 'better-error' of https://bitbucket.org/yackob03/quay into better-error
Conflicts: util/http.py
This commit is contained in:
commit
9650c1867b
4 changed files with 53 additions and 38 deletions
|
@ -75,7 +75,7 @@ def endpoint_invalid_request(e):
|
||||||
def request_error(exception=None, **kwargs):
|
def request_error(exception=None, **kwargs):
|
||||||
data = kwargs.copy()
|
data = kwargs.copy()
|
||||||
if exception:
|
if exception:
|
||||||
data['message'] = ex.message
|
data['message'] = exception.message
|
||||||
|
|
||||||
return make_response(jsonify(data), 400)
|
return make_response(jsonify(data), 400)
|
||||||
|
|
||||||
|
@ -254,12 +254,14 @@ def convert_user_to_organization():
|
||||||
# Ensure that the new admin user is the not user being converted.
|
# Ensure that the new admin user is the not user being converted.
|
||||||
admin_username = convert_data['adminUser']
|
admin_username = convert_data['adminUser']
|
||||||
if admin_username == user.username:
|
if admin_username == user.username:
|
||||||
return request_error(reason = 'invaliduser', message = 'The admin user is not valid')
|
return request_error(reason='invaliduser',
|
||||||
|
message='The admin user is not valid')
|
||||||
|
|
||||||
# Ensure that the sign in credentials work.
|
# Ensure that the sign in credentials work.
|
||||||
admin_password = convert_data['adminPassword']
|
admin_password = convert_data['adminPassword']
|
||||||
if not model.verify_user(admin_username, admin_password):
|
if not model.verify_user(admin_username, admin_password):
|
||||||
return request_error(reason = 'invaliduser', message = 'The admin user credentials are not valid')
|
return request_error(reason='invaliduser',
|
||||||
|
message='The admin user credentials are not valid')
|
||||||
|
|
||||||
# Subscribe the organization to the new plan.
|
# Subscribe the organization to the new plan.
|
||||||
plan = convert_data['plan']
|
plan = convert_data['plan']
|
||||||
|
@ -297,7 +299,8 @@ def change_user_details():
|
||||||
# Email already used.
|
# Email already used.
|
||||||
return request_error(message='E-mail address already used')
|
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)
|
code = model.create_confirm_email_code(user, new_email=new_email)
|
||||||
send_change_email(user.username, user_data['email'], code.code)
|
send_change_email(user.username, user_data['email'], code.code)
|
||||||
|
|
||||||
|
@ -467,7 +470,8 @@ def create_organization():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if existing:
|
if existing:
|
||||||
return request_error(message = 'A user or organization with this name already exists')
|
msg = 'A user or organization with this name already exists'
|
||||||
|
return request_error(message=msg)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
model.create_organization(org_data['name'], org_data['email'],
|
model.create_organization(org_data['name'], org_data['email'],
|
||||||
|
@ -614,7 +618,8 @@ def create_organization_prototype_permission(orgname):
|
||||||
details = request.get_json()
|
details = request.get_json()
|
||||||
activating_username = None
|
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']
|
activating_username = details['activating_user']['name']
|
||||||
|
|
||||||
delegate = details['delegate']
|
delegate = details['delegate']
|
||||||
|
@ -1007,7 +1012,7 @@ def list_repos():
|
||||||
if page:
|
if page:
|
||||||
try:
|
try:
|
||||||
page = int(page)
|
page = int(page)
|
||||||
except:
|
except Exception:
|
||||||
page = None
|
page = None
|
||||||
|
|
||||||
username = None
|
username = None
|
||||||
|
|
|
@ -2,12 +2,12 @@ import json
|
||||||
import logging
|
import logging
|
||||||
import urlparse
|
import urlparse
|
||||||
|
|
||||||
from flask import request, make_response, jsonify, abort as flask_abort, session, Blueprint
|
from flask import request, make_response, jsonify, session, Blueprint
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
|
||||||
from data import model
|
from data import model
|
||||||
from data.queue import webhook_queue
|
from data.queue import webhook_queue
|
||||||
from app import app, mixpanel
|
from app import mixpanel
|
||||||
from auth.auth import (process_auth, get_authenticated_user,
|
from auth.auth import (process_auth, get_authenticated_user,
|
||||||
get_validated_token)
|
get_validated_token)
|
||||||
from util.names import parse_repository_name
|
from util.names import parse_repository_name
|
||||||
|
@ -15,9 +15,9 @@ from util.email import send_confirmation_email
|
||||||
from auth.permissions import (ModifyRepositoryPermission, UserPermission,
|
from auth.permissions import (ModifyRepositoryPermission, UserPermission,
|
||||||
ReadRepositoryPermission,
|
ReadRepositoryPermission,
|
||||||
CreateRepositoryPermission)
|
CreateRepositoryPermission)
|
||||||
|
|
||||||
from util.http import abort
|
from util.http import abort
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
index = Blueprint('index', __name__)
|
index = Blueprint('index', __name__)
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
import logging
|
import logging
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from flask import (make_response, request, session, Response, abort as flask_abort,
|
from flask import (make_response, request, session, Response, redirect,
|
||||||
redirect, Blueprint)
|
Blueprint)
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from time import time
|
from time import time
|
||||||
|
@ -59,7 +59,8 @@ def require_completion(f):
|
||||||
def wrapper(namespace, repository, *args, **kwargs):
|
def wrapper(namespace, repository, *args, **kwargs):
|
||||||
if store.exists(store.image_mark_path(namespace, repository,
|
if store.exists(store.image_mark_path(namespace, repository,
|
||||||
kwargs['image_id'])):
|
kwargs['image_id'])):
|
||||||
abort(400, 'Image %(image_id)s is being uploaded, retry later', image_id=kwargs['image_id'])
|
abort(400, 'Image %(image_id)s is being uploaded, retry later',
|
||||||
|
image_id=kwargs['image_id'])
|
||||||
|
|
||||||
return f(namespace, repository, *args, **kwargs)
|
return f(namespace, repository, *args, **kwargs)
|
||||||
return wrapper
|
return wrapper
|
||||||
|
@ -194,7 +195,8 @@ def put_image_checksum(namespace, repository, image_id):
|
||||||
abort(400, "Missing checksum for image %(image_id)s", image_id=image_id)
|
abort(400, "Missing checksum for image %(image_id)s", image_id=image_id)
|
||||||
|
|
||||||
if not session.get('checksum'):
|
if not session.get('checksum'):
|
||||||
abort(400, 'Checksum not found in Cookie for image %(imaage_id)s', image_id=image_id)
|
abort(400, 'Checksum not found in Cookie for image %(imaage_id)s',
|
||||||
|
image_id=image_id)
|
||||||
|
|
||||||
if not store.exists(store.image_json_path(namespace, repository, image_id)):
|
if not store.exists(store.image_json_path(namespace, repository, image_id)):
|
||||||
abort(404, 'Image not found: %(image_id)s', image_id=image_id)
|
abort(404, 'Image not found: %(image_id)s', image_id=image_id)
|
||||||
|
@ -321,10 +323,12 @@ def put_image_json(namespace, repository, image_id):
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
pass
|
pass
|
||||||
if not data or not isinstance(data, dict):
|
if not data or not isinstance(data, dict):
|
||||||
abort(400, 'Invalid JSON for image: %(image_id)s\nJSON: %(json)s', image_id=image_id, json=request.data)
|
abort(400, 'Invalid JSON for image: %(image_id)s\nJSON: %(json)s',
|
||||||
|
image_id=image_id, json=request.data)
|
||||||
|
|
||||||
if 'id' not in data:
|
if 'id' not in data:
|
||||||
abort(400, 'Missing key `id` in JSON for image: %(image_id)s', image_id=image_id)
|
abort(400, 'Missing key `id` in JSON for image: %(image_id)s',
|
||||||
|
image_id=image_id)
|
||||||
|
|
||||||
# Read the checksum
|
# Read the checksum
|
||||||
checksum = request.headers.get('X-Docker-Checksum')
|
checksum = request.headers.get('X-Docker-Checksum')
|
||||||
|
@ -338,10 +342,12 @@ def put_image_json(namespace, repository, image_id):
|
||||||
# We cleanup any old checksum in case it's a retry after a fail
|
# We cleanup any old checksum in case it's a retry after a fail
|
||||||
store.remove(store.image_checksum_path(namespace, repository, image_id))
|
store.remove(store.image_checksum_path(namespace, repository, image_id))
|
||||||
if image_id != data['id']:
|
if image_id != data['id']:
|
||||||
abort(400, 'JSON data contains invalid id for image: %(image_id)s', image_id=image_id)
|
abort(400, 'JSON data contains invalid id for image: %(image_id)s',
|
||||||
|
image_id=image_id)
|
||||||
|
|
||||||
parent_id = data.get('parent')
|
parent_id = data.get('parent')
|
||||||
if parent_id and not store.exists(store.image_json_path(namespace, repository, parent_id)):
|
if (parent_id and not
|
||||||
|
store.exists(store.image_json_path(namespace, repository, parent_id))):
|
||||||
abort(400, 'Image %(image_id)s depends on non existing parent image %(parent_id)s',
|
abort(400, 'Image %(image_id)s depends on non existing parent image %(parent_id)s',
|
||||||
image_id=image_id, parent_id=parent_id)
|
image_id=image_id, parent_id=parent_id)
|
||||||
|
|
||||||
|
|
16
util/http.py
16
util/http.py
|
@ -1,11 +1,12 @@
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from app import mixpanel
|
from app import mixpanel
|
||||||
from flask import request, abort as flask_abort, make_response
|
from flask import request, abort as flask_abort, jsonify
|
||||||
from auth.auth import process_auth, extract_namespace_repo_from_session, get_authenticated_user, get_validated_token
|
from auth.auth import get_authenticated_user, get_validated_token
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_MESSAGE = {}
|
DEFAULT_MESSAGE = {}
|
||||||
DEFAULT_MESSAGE[400] = 'Invalid Request'
|
DEFAULT_MESSAGE[400] = 'Invalid Request'
|
||||||
DEFAULT_MESSAGE[401] = 'Unauthorized'
|
DEFAULT_MESSAGE[401] = 'Unauthorized'
|
||||||
|
@ -14,7 +15,8 @@ DEFAULT_MESSAGE[404] = 'Not Found'
|
||||||
DEFAULT_MESSAGE[409] = 'Conflict'
|
DEFAULT_MESSAGE[409] = 'Conflict'
|
||||||
|
|
||||||
def abort(status_code, message=None, **kwargs):
|
def abort(status_code, message=None, **kwargs):
|
||||||
message = str(message) % kwargs if message else DEFAULT_MESSAGE.get(status_code, '')
|
message = (str(message) % kwargs if message else
|
||||||
|
DEFAULT_MESSAGE.get(status_code, ''))
|
||||||
|
|
||||||
params = dict(request.view_args)
|
params = dict(request.view_args)
|
||||||
params.copy(kwargs)
|
params.copy(kwargs)
|
||||||
|
@ -27,11 +29,13 @@ def abort(status_code, message=None, **kwargs):
|
||||||
message = '%s (user: %s)' % (message, auth_user.username)
|
message = '%s (user: %s)' % (message, auth_user.username)
|
||||||
elif auth_token:
|
elif auth_token:
|
||||||
mixpanel.track(auth_token.core, 'http_error', params)
|
mixpanel.track(auth_token.core, 'http_error', params)
|
||||||
message = '%s (token: %s)' % (message, auth_token.friendly_name or auth_token.code)
|
message = '%s (token: %s)' % (message,
|
||||||
|
auth_token.friendly_name or auth_token.code)
|
||||||
|
|
||||||
# Log the abort.
|
# Log the abort.
|
||||||
logger.error('Error %s: %s. Arguments: %s' % (status_code, message, params))
|
logger.error('Error %s: %s. Arguments: %s' % (status_code, message, params))
|
||||||
|
resp = jsonify({'error': message})
|
||||||
|
resp.status_code = status_code
|
||||||
|
|
||||||
# Report the abort to the user.
|
# Report the abort to the user.
|
||||||
flask_abort(make_response(message, status_code, {}))
|
flask_abort(resp)
|
||||||
|
|
||||||
|
|
Reference in a new issue