2014-01-24 20:09:35 +00:00
|
|
|
import logging
|
|
|
|
|
2014-01-25 01:29:25 +00:00
|
|
|
from app import mixpanel
|
2014-01-24 22:00:42 +00:00
|
|
|
from flask import request, abort as flask_abort, jsonify
|
|
|
|
from auth.auth import get_authenticated_user, get_validated_token
|
2014-01-24 20:01:40 +00:00
|
|
|
|
2014-01-24 20:09:35 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2014-01-24 22:00:42 +00:00
|
|
|
|
2014-01-24 20:01:40 +00:00
|
|
|
DEFAULT_MESSAGE = {}
|
|
|
|
DEFAULT_MESSAGE[400] = 'Invalid Request'
|
2014-01-24 20:26:32 +00:00
|
|
|
DEFAULT_MESSAGE[401] = 'Unauthorized'
|
2014-01-24 20:17:00 +00:00
|
|
|
DEFAULT_MESSAGE[403] = 'Permission Denied'
|
2014-01-24 20:01:40 +00:00
|
|
|
DEFAULT_MESSAGE[404] = 'Not Found'
|
2014-01-24 20:26:32 +00:00
|
|
|
DEFAULT_MESSAGE[409] = 'Conflict'
|
2014-01-24 20:01:40 +00:00
|
|
|
|
|
|
|
def abort(status_code, message=None, **kwargs):
|
2014-01-24 22:00:42 +00:00
|
|
|
message = (str(message) % kwargs if message else
|
|
|
|
DEFAULT_MESSAGE.get(status_code, ''))
|
2014-01-24 20:17:00 +00:00
|
|
|
|
2014-01-25 01:29:25 +00:00
|
|
|
params = dict(request.view_args)
|
2014-01-25 01:33:42 +00:00
|
|
|
params.update(kwargs)
|
2014-01-25 01:29:25 +00:00
|
|
|
|
2014-01-24 20:26:32 +00:00
|
|
|
# Add the user information.
|
|
|
|
auth_user = get_authenticated_user()
|
|
|
|
auth_token = get_validated_token()
|
|
|
|
if auth_user:
|
2014-01-25 01:29:25 +00:00
|
|
|
mixpanel.track(auth_user.username, 'http_error', params)
|
2014-01-24 20:26:32 +00:00
|
|
|
message = '%s (user: %s)' % (message, auth_user.username)
|
|
|
|
elif auth_token:
|
2014-01-25 01:29:25 +00:00
|
|
|
mixpanel.track(auth_token.core, 'http_error', params)
|
2014-01-24 22:00:42 +00:00
|
|
|
message = '%s (token: %s)' % (message,
|
|
|
|
auth_token.friendly_name or auth_token.code)
|
2014-01-24 20:01:40 +00:00
|
|
|
|
|
|
|
# Log the abort.
|
2014-01-25 01:29:25 +00:00
|
|
|
logger.error('Error %s: %s. Arguments: %s' % (status_code, message, params))
|
2014-01-24 22:00:42 +00:00
|
|
|
resp = jsonify({'error': message})
|
|
|
|
resp.status_code = status_code
|
2014-01-24 20:01:40 +00:00
|
|
|
|
|
|
|
# Report the abort to the user.
|
2014-01-24 22:00:42 +00:00
|
|
|
flask_abort(resp)
|