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

Report user errors in filter as HTTP 400 Bad Request and not 501 Not Implemented #658

Merged
merged 2 commits into from Mar 8, 2021

Conversation

markus1978
Copy link
Contributor

There are different causes for filtertransformer failure that need to be treated differently. Mostly its about raising NotImplementedError (-> 501 Not Implemented) if a certain filter language feature is not supported, but there is also the case where the user causes static semantic errors, e.g. using wrong property names, using operators on properties where they are not allowed (-> 400 Bad Request).

I saw that the mongo transformer was raising a optimade.server.exception.BadRequest to indicate a user error and then was copying this for the elastictransformer. However, all exceptions during a lark tree visit are wrapped in a VisitError and the exception handler (optimade.server.exception_handlers.grammar_not_implemented_handler) is handling all these errors with a 501/Not implemented.

This PR adds a special treatment for BadRequest wrapped in a VisitError.

@codecov
Copy link

codecov bot commented Jan 5, 2021

Codecov Report

Merging #658 (f77235d) into master (7791a44) will decrease coverage by 0.01%.
The diff coverage is 45.83%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #658      +/-   ##
==========================================
- Coverage   92.81%   92.79%   -0.02%     
==========================================
  Files          67       67              
  Lines        3520     3526       +6     
==========================================
+ Hits         3267     3272       +5     
- Misses        253      254       +1     
Flag Coverage Δ
project 92.79% <45.83%> (-0.02%) ⬇️
validator 63.64% <29.16%> (-0.09%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
optimade/server/exceptions.py 94.11% <0.00%> (ø)
optimade/filtertransformers/elasticsearch.py 89.21% <8.33%> (+0.05%) ⬆️
optimade/server/exception_handlers.py 83.09% <90.90%> (-0.24%) ⬇️

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 7791a44...f77235d. Read the comment docs.

Copy link
Member

@CasperWA CasperWA left a comment

Choose a reason for hiding this comment

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

I don't see any issues with this, thanks @markus1978 !

I only have some comments/questions:
Instead of bypassing the rest of the function, I guess you could just one could simply utilize orig_exc attributes for status and title below as well, falling back to the current values?
What do you think?
This should make it more flexible when it comes to introducing other exceptions in the future for the Lark grammar handlers.

Also, I wouldn't be averse to a test of this :) E.g. passing different Exceptions to the exception handler function and checking the outputs?

@CasperWA
Copy link
Member

CasperWA commented Mar 4, 2021

Ready for review in my eyes @ml-evs

ml-evs
ml-evs previously approved these changes Mar 8, 2021
Copy link
Member

@ml-evs ml-evs left a comment

Choose a reason for hiding this comment

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

This looks fine to me, thanks @markus1978 and @CasperWA.

@CasperWA: rebase/merge as you wish after updating the branch, you should be able to self-accept and merge as its not your PR.

markus1978 and others added 2 commits March 8, 2021 19:56
- Return correct str value for custom exceptions
  The StrReprMixin used for custom exceptions has its magic method
  __str__ updated to directly return `detail` if set, else return
  __repr__.
- Remove use of general Exception in elasticsearch.
- Update doc strings for exception handlers.
@CasperWA CasperWA merged commit f77235d into Materials-Consortia:master Mar 8, 2021
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.

None yet

3 participants