Merge branch 'better-error' of https://bitbucket.org/yackob03/quay into better-error

Conflicts:
	util/http.py
This commit is contained in:
Joseph Schorr 2014-01-24 20:32:14 -05:00
commit 9650c1867b
4 changed files with 53 additions and 38 deletions

View file

@ -75,7 +75,7 @@ def endpoint_invalid_request(e):
def request_error(exception=None, **kwargs):
data = kwargs.copy()
if exception:
data['message'] = ex.message
data['message'] = exception.message
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.
admin_username = convert_data['adminUser']
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.
admin_password = convert_data['adminPassword']
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.
plan = convert_data['plan']
@ -297,7 +299,8 @@ def change_user_details():
# Email 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)
send_change_email(user.username, user_data['email'], code.code)
@ -467,7 +470,8 @@ def create_organization():
pass
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:
model.create_organization(org_data['name'], org_data['email'],
@ -614,7 +618,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']
@ -1007,7 +1012,7 @@ def list_repos():
if page:
try:
page = int(page)
except:
except Exception:
page = None
username = None

View file

@ -2,12 +2,12 @@ import json
import logging
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 data import model
from data.queue import webhook_queue
from app import app, mixpanel
from app import mixpanel
from auth.auth import (process_auth, get_authenticated_user,
get_validated_token)
from util.names import parse_repository_name
@ -15,9 +15,9 @@ from util.email import send_confirmation_email
from auth.permissions import (ModifyRepositoryPermission, UserPermission,
ReadRepositoryPermission,
CreateRepositoryPermission)
from util.http import abort
logger = logging.getLogger(__name__)
index = Blueprint('index', __name__)

View file

@ -1,8 +1,8 @@
import logging
import json
from flask import (make_response, request, session, Response, abort as flask_abort,
redirect, Blueprint)
from flask import (make_response, request, session, Response, redirect,
Blueprint)
from functools import wraps
from datetime import datetime
from time import time
@ -59,7 +59,8 @@ def require_completion(f):
def wrapper(namespace, repository, *args, **kwargs):
if store.exists(store.image_mark_path(namespace, repository,
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 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)
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)):
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:
pass
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:
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
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
store.remove(store.image_checksum_path(namespace, repository, image_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')
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',
image_id=image_id, parent_id=parent_id)

View file

@ -1,11 +1,12 @@
import logging
from app import mixpanel
from flask import request, abort as flask_abort, make_response
from auth.auth import process_auth, extract_namespace_repo_from_session, get_authenticated_user, get_validated_token
from flask import request, abort as flask_abort, jsonify
from auth.auth import get_authenticated_user, get_validated_token
logger = logging.getLogger(__name__)
DEFAULT_MESSAGE = {}
DEFAULT_MESSAGE[400] = 'Invalid Request'
DEFAULT_MESSAGE[401] = 'Unauthorized'
@ -14,7 +15,8 @@ DEFAULT_MESSAGE[404] = 'Not Found'
DEFAULT_MESSAGE[409] = 'Conflict'
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.copy(kwargs)
@ -27,11 +29,13 @@ def abort(status_code, message=None, **kwargs):
message = '%s (user: %s)' % (message, auth_user.username)
elif auth_token:
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.
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.
flask_abort(make_response(message, status_code, {}))
flask_abort(resp)