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

fix: Added error handler for status 429 #6678

Merged
merged 29 commits into from Jan 13, 2020
Merged

fix: Added error handler for status 429 #6678

merged 29 commits into from Jan 13, 2020

Conversation

Rits1272
Copy link
Member

Fixes #6659

Short description of what this resolves: Added Error handler for status 429 in the errors.py file

  • I have read the Contribution & Best practices Guide and my PR follows them.
  • My branch is up-to-date with the Upstream development branch.
  • The unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)
  • All the functions created/modified in this PR contain relevant docstrings.

@Rits1272
Copy link
Member Author

Please review @codedsun

@codecov
Copy link

codecov bot commented Dec 19, 2019

Codecov Report

Merging #6678 into development will decrease coverage by 0.2%.
The diff coverage is 20.54%.

Impacted file tree graph

@@               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
Impacted Files Coverage Δ
app/instance.py 91.3% <0%> (+0.49%) ⬆️
app/models/order.py 90.19% <100%> (+0.19%) ⬆️
app/api/custom/orders.py 26.89% <18.46%> (-14.38%) ⬇️
app/api/helpers/order.py 23.2% <20%> (-0.77%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4ca8411...9c60849. Read the comment docs.

Copy link
Contributor

@codedsun codedsun left a 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

@Rits1272
Copy link
Member Author

Kindly review again @codedsun

Copy link
Member

@kushthedude kushthedude left a 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.

@kushthedude
Copy link
Member

Also, Squash your commits.

mrsaicharan1
mrsaicharan1 previously approved these changes Dec 20, 2019
@Rits1272
Copy link
Member Author

Sure @kushthedude and thanks for reviewing @codedsun, @kushthedude and @mrsaicharan1!

@Rits1272 Rits1272 changed the title Added error handler for status 429 feat : Added error handler for status 429 Dec 20, 2019
@auto-label auto-label bot added the feature label Dec 20, 2019
@Rits1272
Copy link
Member Author

@kushthedude I have changed the PR title in accordance with semantic PR naming and also squashed the commits.

Copy link
Contributor

@codedsun codedsun left a 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

app/__init__.py Outdated Show resolved Hide resolved
For pep8 style

Registered error handler

Registered error handler

update

Added and registered error handler for status 429
@Rits1272
Copy link
Member Author

Please @codedsun check. I think I have registered the error handler successfully

app/api/helpers/errors.py Outdated Show resolved Hide resolved

@error_blueprint.app_errorhandler(429)
def handle_429(error):
raise RequestLimitError({'source': ''}, error.description).respond()
Copy link
Member

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

Copy link
Member Author

@Rits1272 Rits1272 Dec 20, 2019

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

Copy link
Member

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

Copy link
Member Author

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!

Default class for 429 Error
"""
status = 429
title = 'Request Limit Exceeded'
Copy link
Member

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

Copy link
Member Author

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed

Copy link
Member Author

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 Show resolved Hide resolved
@@ -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

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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whitespace before ','

@@ -77,4 +76,4 @@ class BadRequestError(ErrorResponse):
Default class for 400 Error
"""
status = 400
title = 'Bad Request'
title = 'Bad Request'

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()

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

kushthedude and others added 8 commits December 22, 2019 11:32
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
@@ -15,7 +15,7 @@ class ErrorResponse:

def __init__(self, source, detail, title=None, status=None):
"""Initialize a jsonapi ErrorResponse Object

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 Show resolved Hide resolved
app/__init__.py Outdated Show resolved Hide resolved
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'})
Copy link
Member

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?

Copy link
Member Author

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.

@Rits1272
Copy link
Member Author

Rits1272 commented Jan 6, 2020

Please review @iamareebjamal

@iamareebjamal
Copy link
Member

No need for jsonapi errors, source or details

@Rits1272
Copy link
Member Author

Rits1272 commented Jan 9, 2020

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'})
Copy link
Member

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay

@Rits1272
Copy link
Member Author

I made the suggested changes @iamareebjamal

app/instance.py Outdated
'status': 429,
'title': 'Request Limit Exceeded'
}),
429, {
Copy link
Member

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

@Rits1272
Copy link
Member Author

Made the changes @iamareebjamal

@iamareebjamal iamareebjamal changed the title feat : Added error handler for status 429 fix: Added error handler for status 429 Jan 10, 2020
@auto-label auto-label bot added fix and removed feature labels Jan 10, 2020
Copy link
Member

@mrsaicharan1 mrsaicharan1 left a 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!

@Rits1272
Copy link
Member Author

Thanks a lot, @mrsaicharan1 and @iamareebjamal for having patience with me. I will try my best in improving my PR from next time.

@iamareebjamal iamareebjamal merged commit 3fad651 into fossasia:development Jan 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add error handler for status 429
7 participants