Skip to content

Fix #929 - Make Error management more DRY #958

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 15, 2016
6 changes: 6 additions & 0 deletions tests/test_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,15 @@ def test_login(self):
self.assertIn('github.com/login/oauth/', rv.headers['Location'])

def test_activity_page_401_if_not_logged_in(self):
'''Test that asks user to log in before displaying activity.'''
rv = self.app.get('/me')
self.assertEqual(rv.status_code, 401)

def test_activity_page_403_view_other_user_activity(self):
'''Test to check that another user activity is not displayed.'''
rv = self.app.get('/activity/random_user_not_me')
self.assertEqual(rv.status_code, 403)

def test_issue_int(self):
'''Test issues and integer for:

Expand Down
111 changes: 38 additions & 73 deletions webcompat/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@
from webcompat.api.uploads import upload


ERROR_DICT = {400: 'Bad Request.',
401: 'Unauthorized. Please log in.',
403: 'Forbidden. Maybe that looking at private stuff?',
404: 'Not Found. Lost in Punk Cat Space',
429: 'Cool your jets! Please wait {0} seconds before making'
'another search.',

This comment was marked as abuse.

500: 'Internal Server Error',
GitHubError: 'Something bad happened. Please try again?'}

This comment was marked as abuse.



@app.teardown_appcontext
def shutdown_session(exception=None):
session_db.remove()
Expand Down Expand Up @@ -291,80 +301,43 @@ def cssfixme():
def jumpship(e):
print('jumpship! ', e)
session.pop('user_id', None)
flash('Something bad happened. Please try again?', 'error')
flash(ERROR_DICT[GitHubError], 'error')
return redirect(url_for('index'))


@app.errorhandler(400)
def bad_request_status(err):
message = 'Bad Request.'
if (request.path.startswith('/api/') and
request.accept_mimetypes.accept_json and
not request.accept_mimetypes.accept_html):
message = {
'status': 400,
'message': 'API call. ' + message,
}
resp = jsonify(message)
resp.status_code = 400
return resp
return render_template('error.html',
error_code=400,
error_message=message), 400


@app.errorhandler(401)
def unauthorized_status(err):
message = 'Unauthorized. Please log in.'
if (request.path.startswith('/api/') and
request.accept_mimetypes.accept_json and
not request.accept_mimetypes.accept_html):
message = {
'status': 401,
'message': 'API call. ' + message,
}
resp = jsonify(message)
resp.status_code = 401
return resp
return render_template('error.html',
error_code=401,
error_message=message), 401
@app.errorhandler(403)
@app.errorhandler(404)
@app.errorhandler(500)
def custom_error_handler(err):
if api_call(request):
return api_message(err.code)
return render_template(
'error.html',
error_code=err.code,
error_message=ERROR_DICT[err.code]), err.code


@app.errorhandler(403)
def forbidden_status(err):
message = 'Forbidden. Are you trying to look at someone else\'s stuff?'
def api_call(request):
'''Checks if it's an API call'''
if (request.path.startswith('/api/') and
request.accept_mimetypes.accept_json and
not request.accept_mimetypes.accept_html):
message = {
'status': 403,
'message': 'API call. ' + message,
}
resp = jsonify(message)
resp.status_code = 403
return resp
return render_template('error.html',
error_code=403,
error_message=message), 403
return True
else:
return False


@app.errorhandler(404)
def not_found_status(err):
if (request.path.startswith('/api/') and
request.accept_mimetypes.accept_json and
not request.accept_mimetypes.accept_html):
message = {
'status': 404,
'message': 'API call. Not Found',
}
resp = jsonify(message)
resp.status_code = 404
return resp
message = "We can't find what you are looking for."
return render_template('error.html',
error_code=404,
error_message=message), 404
def api_message(code):
'''Prepares HTTP response for API calls.'''
message = {
'status': code,
'message': ERROR_DICT[code],
}
resp = jsonify(message)
resp.status_code = 404
return resp


@app.errorhandler(429)
Expand All @@ -379,15 +352,7 @@ def too_many_requests_status(err):
# TODO: determine actual time left.
# TODO: send message with login link.
time_left = 60
message = ('Cool your jets! Please wait {0} seconds before making'
' another search.').format(time_left)
message = (ERROR_DICT[err.code]).format(time_left)
error_data = {'message': message, 'timeout': 5}
return (json.dumps(error_data), 429, {'content-type': 'application/json'})


@app.errorhandler(500)
def internal_server_error_status(err):
message = "Internal Server Error"
return render_template('error.html',
error_code=500,
error_message=message), 500
return (json.dumps(error_data),
err.code, {'content-type': 'application/json'})