-
Notifications
You must be signed in to change notification settings - Fork 203
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
Fixes #880. Allow visiting /activity/username route directly. #928
Conversation
This won't work now for seeing other people's activity, but before you just got an error page. This also fixes the bug where logging in after the error had a missing avatar image.
Let's not merge yet, I should write a test or two for this. |
r+ @miketaylr not to self. Need to refactor this errorhandler think. We are reusing the same code over and over again. 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 More something like (but to think not completely satisfied with the code below) 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',
}
@app.errorhandler(400)
@app.errorhandler(401)
@app.errorhandler(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])
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):
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 = 404
return resp |
thanks @karlcow -- I agree, we could abstract the error handling into a nice little method. Will fix the failing nose test and write some new ones before merging. |
This will get replaced with a functional test that can log in. (the trivial fix would be to change this to expect 401, but that would just be a duplicate of the next test)
OK, tests are passing -- let's merge. Thanks for review @karlcow. |
Fixes #880. Allow visiting /activity/username route directly.
@@ -278,6 +295,24 @@ def jumpship(e): | |||
return redirect(url_for('index')) | |||
|
|||
|
|||
@app.errorhandler(400) | |||
def unauthorized(err): |
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
r? @karlcow
(sorry for taking so long here @magsout)