Skip to content
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

Issue #929 - Generic error handler based on status code for status 40… #949

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions requirements.txt

This file was deleted.

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
98 changes: 36 additions & 62 deletions webcompat/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,18 @@ def cssfixme():
'''Route for CSS Fix me tool'''
return render_template('cssfixme.html')

ERROR_DICT = {
400: 'Bad Request.',
401: 'Unauthorized. Please log in.',
403: 'Forbidden. Are you trying to look at someone else\'s stuff?',
<<<<<<< HEAD
404: 'Not Found. Lost in Punk Cat Space'
}
=======
404: 'Not Found. Lost in Punk Cat Space',
}
>>>>>>> 85f2a83bd3ed07e89f23c3990a62e462bc913891


@app.errorhandler(GitHubError)
def jumpship(e):
Expand All @@ -296,75 +308,37 @@ def jumpship(e):


@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)
def forbidden_status(err):
message = 'Forbidden. Are you trying to look at someone else\'s stuff?'
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
@app.errorhandler(404)
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(404)
def not_found_status(err):
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': 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
return True
else:
return False


def api_message(code):
'''Prepares HTTP response for API calls.'''
message = {
'status': code,
'message': ERROR_DICT[code],
}
resp = jsonify(message)
resp.status_code = code
return resp


@app.errorhandler(429)
Expand Down