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

New middleware to catch any OptimadeWarnings #431

Merged
merged 10 commits into from
Jul 29, 2020

Conversation

CasperWA
Copy link
Member

@CasperWA CasperWA commented Jul 27, 2020

This is put as the first added middleware, since the order in which middleware is added to the application matters.
See https://www.starlette.io/middleware/

The middleware is a bit hacky, since it's based on Starlette's BaseHTTPMiddleware, which uses the call_next function that creates a StreamingResponse.
This middleware will temporarily stitch the response back together, add the warnings, and chop up the response again, returning a new StreamingResponse.

Another thing that's a bit hacky about this is the way in which warnings are collected. I currently use an in-memory attribute from the middleware class, however, I am not sure if this is thread-safe.

Note, I have tested this by raising a simple warning in a router and it seemed to work :)

Closes #105


Missing:

  • Tests
  • Add in all "TODO" warnings littered around in the code.
  • Tests for ^these warnings.

This is put as the first added middleware, since the order in which
middleware is added to the application matters.
See https://www.starlette.io/middleware/

The middleware is a bit hacky, since it's based on Starlette's
`BaseHTTPMiddleware`, which uses the `call_next` function that creates a
`StreamingResponse`.
This middleware will temporarily stitch the response back together, add
the warnings, and chop up the response again, returning a new
StreamingResponse.
@codecov
Copy link

codecov bot commented Jul 27, 2020

Codecov Report

Merging #431 into master will decrease coverage by 0.13%.
The diff coverage is 83.33%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #431      +/-   ##
==========================================
- Coverage   91.09%   90.95%   -0.14%     
==========================================
  Files          55       56       +1     
  Lines        2548     2632      +84     
==========================================
+ Hits         2321     2394      +73     
- Misses        227      238      +11     
Flag Coverage Δ
#unittests 90.95% <83.33%> (-0.14%) ⬇️

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

Impacted Files Coverage Δ
optimade/adapters/structures/proteindatabank.py 100.00% <ø> (ø)
optimade/server/middleware.py 88.39% <81.69%> (-11.61%) ⬇️
optimade/server/warnings.py 84.61% <84.61%> (ø)
optimade/server/exception_handlers.py 82.53% <100.00%> (+2.23%) ⬆️
optimade/server/main.py 95.91% <100.00%> (+0.08%) ⬆️
optimade/server/main_index.py 95.91% <100.00%> (+0.08%) ⬆️
optimade/server/routers/utils.py 91.09% <100.00%> (+0.06%) ⬆️
optimade/models/optimade_json.py 96.51% <0.00%> (+2.32%) ⬆️

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 15a5f7a...f830883. Read the comment docs.

Warning needs the message to be a string, not None.
Test "regular" warnings are still handled fine
It seemed the places I could find to add warnings to didn't need it.
Instead there are now two new Warning classes, which didn't need using,
but may still be usable in other situations.
@CasperWA CasperWA marked this pull request as ready for review July 28, 2020 01:52
@CasperWA CasperWA requested review from ml-evs and shyamd July 28, 2020 01:52
@CasperWA
Copy link
Member Author

I couldn't find more places to add warnings. Actually, I couldn't find any. So please feel free to add them where you think they're needed.

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.

Looks great @CasperWA, nice one. Not sure I follow every line in the middleware, but I trust the tests. My only request is that you expand on one of the docstrings, ping me to approve when you're done.

optimade/server/middleware.py Outdated Show resolved Hide resolved
optimade/server/routers/utils.py Show resolved Hide resolved
optimade/server/warnings.py Show resolved Hide resolved
@CasperWA CasperWA requested a review from ml-evs July 29, 2020 15:27
@CasperWA
Copy link
Member Author

By the way, the codecov diff is bad due to mainly no warnings being used in the servers so far. Hence, I cannot test for them to exist. But as soon as we can introduce a warning somewhere, this will be properly tested as well. It seems a bit backward, but oh well, I couldn't come up with a good way of testing this in situ otherwise.

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.

Thanks for the explanations!

Minor thing, had some trouble building the docs locally for this PR (to check the docstring links); let's see how the CI handles it, we can add any fixes to #430

@ml-evs ml-evs merged commit 94720cc into master Jul 29, 2020
@ml-evs ml-evs deleted the close_105_implement-warnings branch July 29, 2020 16:12
@CasperWA
Copy link
Member Author

Concerning the docs, It's because the pimped up API reference from #430 needs to be added.
I could have added the optimade.server.warnings module manually in this PR as well, but with #430 open, I considered it unnecessary :)

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.

Implement warnings
2 participants