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

Raise Auth0Error for bad status code #98

Merged
merged 14 commits into from
Apr 27, 2018
Merged

Raise Auth0Error for bad status code #98

merged 14 commits into from
Apr 27, 2018

Conversation

beck3905
Copy link
Contributor

I copied the logic from authentication/base.py for _process_response into management/rest.py. This way any 400 or greater status codes will result in an Auth0Error.

The current functionality, which is based on 'error_code' being present in the response is not good enough because not all Auth0 API error response include 'error_code' and thus bad response get through when the expectation is a valid object. For example, if I call auth0.users.create() and get a 429 response, the API response does not have an error_code and my code expects to receive a valid user profile but instead receives a {"error": "Too Many Requests", "status_code": 429}.

This change will make consuming the auth0-python sdk more consistent as all error responses will result in an Auth0Error instead of some resulting in an Auth0Error and other returning an error object when an Auth0 object is expected.

@codecov-io
Copy link

codecov-io commented Apr 18, 2018

Codecov Report

Merging #98 into master will increase coverage by 0.55%.
The diff coverage is 94.79%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #98      +/-   ##
==========================================
+ Coverage    92.5%   93.05%   +0.55%     
==========================================
  Files          32       32              
  Lines         547      634      +87     
==========================================
+ Hits          506      590      +84     
- Misses         41       44       +3
Impacted Files Coverage Δ
auth0/v3/management/rest.py 83.15% <93.87%> (+9.15%) ⬆️
auth0/v3/authentication/base.py 94.91% <95.74%> (+12.56%) ⬆️

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 c616062...9155777. Read the comment docs.

@beck3905
Copy link
Contributor Author

@cocojoe, @aaguiarz I'm not sure who needs to review this but I see you were the last two to merge pull requests. Could one of you please review this or assign it to the correct person? Thanks.

@cocojoe cocojoe requested a review from lbalmaceda April 19, 2018 10:09
@cocojoe
Copy link
Member

cocojoe commented Apr 19, 2018

Thanks, will do.

Copy link
Contributor

@lbalmaceda lbalmaceda left a comment

Choose a reason for hiding this comment

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

There's another open PR that fixes this very same thing but for authentication and includes parsing of "plain text error responses". I'd like to merge that one first and then we can think of management and copy that new base file here.
BTW a similar PR was proposed a few days before yours https://github.com/auth0/auth0-python/pull/97/files. I'll leave this in standby and see later which one to merge.
Thanks for your contribution 🎉

…rror

Correctly throw an exception when handing a text response
Copy link
Contributor

@lbalmaceda lbalmaceda left a comment

Choose a reason for hiding this comment

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

Please rebase and review how the /authentication/rest.py file now handles this error and status codes. You'll probably want to copy that behavior now that is merged instead.

raise Auth0Error(status_code=text['statusCode'],
error_code=text['errorCode'],
error_code=text.get('errorCode', ''),
Copy link
Contributor

Choose a reason for hiding this comment

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

Another PR suggests falling back to the property "error" when "errorCode" is not present in the response.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've looked at the latest change to /authentication/base.py and it mostly looks good because it both takes status_code into account and uses error instead of errorCode. The only issue I noticed is that it is not python 2 compatible. Using super().__init__(...) is not supported in python 2. To be supported in both python 2 and python 3 this should be super(Response, self).__init__(...)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will update my PR with this change in /authentication/base.py and update /management/rest.py to use the same logic.

Copy link
Contributor

Choose a reason for hiding this comment

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

Got it @beck3905, thanks. @darkyen can you please give me a hand reviewing the python side of things here? I don't even know which is the minimum python version we support. I guess from here that we target 2.7 and up.

@lbalmaceda lbalmaceda requested a review from darkyen April 20, 2018 17:49
@beck3905
Copy link
Contributor Author

@darkyen and @lbalmaceda I've updated tests to keep the coverage checks happy. Can you review these changes and let me know if they can be incorporated into the SDK? Thanks

Copy link

@darkyen darkyen 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 to me, thanks a lot for the great work @beck3905 :).

After this is merged though, we should break out errors as Auth0ManagementError and Auth0AuthenticationError

@beck3905
Copy link
Contributor Author

@darkyen Thanks for the approval. Any idea on the timeline for when this will be merged and available in a new version of the auth0-python package?

@lbalmaceda
Copy link
Contributor

I'll try to make a new release by Friday.

@lbalmaceda lbalmaceda merged commit 6484229 into auth0:master Apr 27, 2018
@lbalmaceda
Copy link
Contributor

lbalmaceda commented Apr 27, 2018

@beck3905 I've just uploaded 3.2.0. It was my first time uploading a package to pypi so please, share feedback whether it works and/or is available or not. https://pypi.org/project/auth0-python/

@beck3905
Copy link
Contributor Author

beck3905 commented May 2, 2018 via email

@lbalmaceda
Copy link
Contributor

I'll see if I can get any help from the python devs here. Thanks for the feedback @beck3905 ! @darkyen would you help me update the release binaries please?

@beck3905
Copy link
Contributor Author

beck3905 commented May 8, 2018

@lbalmaceda @darkyen - Any updates on this? I'm still not seeing this code when I pip install auth0-python 3.2.0.

@lbalmaceda
Copy link
Contributor

@beck3905 I just uploaded 3.2.2. At least the bdist code looks fine. I can't inspect the wheel one so I can't say. Can you please check ? I'll leave the release steps at the bottom.

Also, I see releases before 3.2.0 had a "dumb binary" file uploaded which I can't seem to generate/upload. I've followed the steps at the distributing packages guide but even if I can generate the sdist I can't seem to upload it, so I skipped that one.

HTTPError: 400 Client Error: Only one sdist may be uploaded per release. for url: https://test.pypi.org/legacy/

Does anyone know if this is the right procedure?

# Generate distribution files
python setup.py sdist bdist

# Generate wheel files
python setup.py bdist_wheel --universal

# Upload files
twine upload dist/*

@lbalmaceda lbalmaceda mentioned this pull request May 9, 2018
@cocojoe cocojoe added this to the v3-Next milestone May 9, 2018
@beck3905
Copy link
Contributor Author

beck3905 commented May 9, 2018

@lbalmaceda I just pip installed the latest package and inspected the rest.py file in my site-packages directory. Looks like it has the changes in 3.2.2. Thanks.

@lbalmaceda lbalmaceda modified the milestones: v3-Next, 3.2.0 Jul 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants