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
fix: Added error handler for status 429 #6678
Conversation
Please review @codedsun |
Codecov Report
@@ Coverage Diff @@
## development #6678 +/- ##
===============================================
- Coverage 65.6% 65.39% -0.21%
===============================================
Files 300 300
Lines 15258 15319 +61
===============================================
+ Hits 10010 10018 +8
- Misses 5248 5301 +53
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also register this error in the app
Kindly review again @codedsun |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Follow Semantic PR Naming for your PR title.
Also, Squash your commits. |
Sure @kushthedude and thanks for reviewing @codedsun, @kushthedude and @mrsaicharan1! |
@kushthedude I have changed the PR title in accordance with semantic PR naming and also squashed the commits. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where are you using theRequestLimitError
? Please register its handler in the app also
For pep8 style Registered error handler Registered error handler update Added and registered error handler for status 429
Please @codedsun check. I think I have registered the error handler successfully |
app/api/helpers/errors.py
Outdated
|
||
@error_blueprint.app_errorhandler(429) | ||
def handle_429(error): | ||
raise RequestLimitError({'source': ''}, error.description).respond() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this? Keep it consistent with other handlers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please tell me where other error handlers are implemented @kushthedude? Because I first tried to find how other handlers were registered but was not able to find them. Also, issue #6665 says to register custom error handlers that means previously no custom error handlers are registered. Please guide me further if I am going wrong
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are present in __init__.py
of app
And you can't just raise in an error handler itself. The raise is caught in error handler so that proper response can be returned from there. The app will crash if you raise in the error handler itself which has caught the error, hence no point of catching at all
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok @iamareebjamal. Working on it. Thanks for reviewing!
app/api/helpers/errors.py
Outdated
Default class for 429 Error | ||
""" | ||
status = 429 | ||
title = 'Request Limit Exceeded' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not needed. Try to understand the issue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure
# Request limit Error | ||
request_limit_error = RequestLimitError({'source': ''}, | ||
'Request limit exceeded') | ||
self.assertEqual(request_limit_error.status, 429) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will remove it
app/api/helpers/errors.py
Outdated
@@ -24,7 +22,7 @@ def __init__(self, source, detail, title=None, status=None): | |||
if title is not None: | |||
self.title = title | |||
if status is not None: | |||
self.status = status | |||
self.status = staRequestLimitErrortus |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
undefined name 'staRequestLimitErrortus'
app/__init__.py
Outdated
@app.errorhandler(429) | ||
def request_limit_error(err): | ||
return make_response( | ||
jsonify(error="ratelimit exceeded %s" % err.description) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whitespace before ','
app/api/helpers/errors.py
Outdated
@@ -77,4 +76,4 @@ class BadRequestError(ErrorResponse): | |||
Default class for 400 Error | |||
""" | |||
status = 400 | |||
title = 'Bad Request' | |||
title = 'Bad Request' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
blank line at end of file
app/__init__.py
Outdated
if __name__ == '__main__': | ||
current_app.run() | ||
current_app.run() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
blank line at end of file
Updates the requirements on [geoip2](https://github.com/maxmind/libmaxminddb) to permit the latest version. - [Release notes](https://github.com/maxmind/libmaxminddb/releases) - [Changelog](https://github.com/maxmind/libmaxminddb/blob/master/Changes.md) - [Commits](https://github.com/maxmind/libmaxminddb/commits) Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
Updates the requirements on [celery](https://github.com/celery/celery) to permit the latest version. - [Release notes](https://github.com/celery/celery/releases) - [Changelog](https://github.com/celery/celery/blob/master/Changelog.rst) - [Commits](celery/celery@v4.3.0...4.4.0) Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
Updates the requirements on [sqlalchemy](https://github.com/sqlalchemy/sqlalchemy) to permit the latest version. - [Release notes](https://github.com/sqlalchemy/sqlalchemy/releases) - [Changelog](https://github.com/sqlalchemy/sqlalchemy/blob/master/CHANGES) - [Commits](https://github.com/sqlalchemy/sqlalchemy/commits) Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
Updates the requirements on [coverage](https://github.com/nedbat/coveragepy) to permit the latest version. - [Release notes](https://github.com/nedbat/coveragepy/releases) - [Changelog](https://github.com/nedbat/coveragepy/blob/master/CHANGES.rst) - [Commits](nedbat/coveragepy@coverage-4.5...coverage-5.0) Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
Added error handler for status 429
app/api/helpers/errors.py
Outdated
@@ -15,7 +15,7 @@ class ErrorResponse: | |||
|
|||
def __init__(self, source, detail, title=None, status=None): | |||
"""Initialize a jsonapi ErrorResponse Object | |||
|
|||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
blank line contains whitespace
app/__init__.py
Outdated
def ratelimit_handler(error): | ||
exc = JsonApiException({'pointer': ''}, str(error)) | ||
return make_response(json.dumps(jsonapi_errors([exc.to_dict()])), exc.status, | ||
{'Content-Type': 'application/vnd.api+json'}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not what we want. Did you even test what it returns?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, Working on it @iamareebjamal.
Please review @iamareebjamal |
No need for jsonapi errors, source or details |
Made the changes @iamareebjamal |
app/instance.py
Outdated
'title': 'Request Limit Exceeded', | ||
} | ||
return make_response(json.dumps(dict_), 429, | ||
{'Content-Type': 'application/vnd.api+json'}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Properly format and indent. Remove dict_ variable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay
I made the suggested changes @iamareebjamal |
app/instance.py
Outdated
'status': 429, | ||
'title': 'Request Limit Exceeded' | ||
}), | ||
429, { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{ should start from next line
Made the changes @iamareebjamal |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good but try to go through the issue thoroughly before sending in a fix from next time. Good start!
Thanks a lot, @mrsaicharan1 and @iamareebjamal for having patience with me. I will try my best in improving my PR from next time. |
Fixes #6659
Short description of what this resolves: Added Error handler for status 429 in the errors.py file
development
branch.