Skip to content

Make error handlers order of registration respected when handling errors #201

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

Merged

Conversation

avilaton
Copy link
Contributor

Using an regular dict on error_handlers, both at Api and at Namespace, mean that which handler was not predictable. We had 2 error handlers, one for a generic service exception and one for a more specific exception that needed special care. Our registration worked some times, but not others, and this is the root cause. Here is a sample of what we had

class GenericError(Exception):
    pass


class SomeSpecificError(Exception):
    pass


ns = Namespace("some_route")

@ns.errorhandler(SomeSpecificError)
def handle_specific_error(error):
    return {"message": "something specific happened"}, 401

@ns.errorhandler(GenericError)
def handle_specific_error(error):
    return {"message": "generic error"}, 401

and a route was raising SomeSpecificError. One sees ocurrences of one or the other message depending on what is returned from the dict were those errors are stored.

@codecov
Copy link

codecov bot commented Aug 13, 2020

Codecov Report

Merging #201 into master will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #201   +/-   ##
=======================================
  Coverage   96.85%   96.85%           
=======================================
  Files          19       19           
  Lines        2705     2705           
=======================================
  Hits         2620     2620           
  Misses         85       85           
Impacted Files Coverage Δ
flask_restx/api.py 96.69% <100.00%> (ø)
flask_restx/namespace.py 94.54% <100.00%> (ø)

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 d62c7da...8a18aa3. Read the comment docs.

@avilaton
Copy link
Contributor Author

Proposed fix for #202

ziirish
ziirish previously approved these changes Sep 2, 2020
@ziirish
Copy link
Contributor

ziirish commented Sep 2, 2020

Thanks, I'll merge this one but this won't be necessary anymore in the future since python 3.7 language specification ensures that dict are now ordered.
It's actually the case in python 3.6 but this was not enforced by the language specs at the time so other python implementation such as PyPy might not implement it the same way.

@ziirish
Copy link
Contributor

ziirish commented Sep 2, 2020

I'm not able to merge it right now, could you please rebase to master?

@avilaton
Copy link
Contributor Author

avilaton commented Sep 3, 2020

I'm not able to merge it right now, could you please rebase to master?

done

@ziirish ziirish merged commit 53a9a4d into python-restx:master Sep 5, 2020
@ziirish
Copy link
Contributor

ziirish commented Sep 5, 2020

Thanks!

@avilaton avilaton deleted the fix-error-handler-order-errors branch September 7, 2020 22:08
@avilaton
Copy link
Contributor Author

avilaton commented Sep 7, 2020

Any ideas of when we could see a release come out?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants