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

Add exception handling to the rest api #3708

Merged
merged 11 commits into from
Oct 30, 2024

Conversation

jpbruinsslot
Copy link
Contributor

@jpbruinsslot jpbruinsslot commented Oct 21, 2024

Changes

  • This PR adds an exception_handler decorator for the rest API in the scheduler. Mainly cleans up repeated code in the scheduler api.
  • Cleaned API tests up a little bit

Issue link

Closes #2994

QA notes

Since I restructured and deleted some code from the rest API, the main gist is that everything should still work with services that interact with the scheduler. Everything should return the correct response and status codes with normal usage.

Perhaps checking if error messages in rocky are relayed correctly when interacting with the scheduler.


Code Checklist

  • All the commits in this PR are properly PGP-signed and verified.
  • This PR only contains functionality relevant to the issue.
  • I have written unit tests for the changes or fixes I made.
  • I have checked the documentation and made changes where necessary.
  • I have performed a self-review of my code and refactored it to the best of my abilities.
  • Tickets have been created for newly discovered issues.
  • For any non-trivial functionality, I have added integration and/or end-to-end tests.
  • I have informed others of any required .env changes files if required and changed the .env-dist accordingly.
  • I have included comments in the code to elaborate on what is not self-evident from the code itself, including references to issues and discussions online, or implicit behavior of an interface.

Checklist for code reviewers:

Copy-paste the checklist from the docs/source/templates folder into your comment.


Checklist for QA:

Copy-paste the checklist from the docs/source/templates folder into your comment.

@jpbruinsslot jpbruinsslot added the mula Issues related to the scheduler label Oct 21, 2024
@jpbruinsslot jpbruinsslot self-assigned this Oct 21, 2024
@jpbruinsslot jpbruinsslot marked this pull request as ready for review October 21, 2024 15:29
@jpbruinsslot jpbruinsslot requested a review from a team as a code owner October 21, 2024 15:29
Comment on lines +62 to +63
[tool.pytest.ini_options]
addopts = "--cov scheduler/ --cov-config=pyproject.toml --cov-report xml --cov-branch --cov-report=term-missing:skip-covered"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added such that coverage from pytest doesn't include unnecessary folders/files to count towards coverage

Copy link
Contributor

@ammar92 ammar92 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, but I was hoping that this #3695 (comment) would be implemented. But perhaps there has been a misunderstanding with the term exception_handler? What I meant was using FastAPI's way of dealing with custom exceptions like this. This is a common and less verbose solution. It works pretty well and we've also used this in other places

jpbruinsslot and others added 2 commits October 23, 2024 13:03
Co-authored-by: Jan Klopper <janklopper+underdark@gmail.com>
ammar92
ammar92 previously approved these changes Oct 23, 2024
Copy link
Contributor

@ammar92 ammar92 left a comment

Choose a reason for hiding this comment

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

Nice work 👍 Tiny remarks that could improve it even more, but consider it approved

Comment on lines 80 to 84
def exception_handler(request: fastapi.Request, exc: Exception):
return JSONResponse(
status_code=fastapi.status.HTTP_500_INTERNAL_SERVER_ERROR,
content={"detail": f"An internal error occurred: {exc}"},
)
Copy link
Contributor

Choose a reason for hiding this comment

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

If I'm not mistaken this isn't needed because it's handled by the default exception handler

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'll check it

Copy link
Contributor

Choose a reason for hiding this comment

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

This is indeed the nicer approach. The only addition here could be importing from FastAPI import status and use status.HTTP_... instead. This would make some lines a bit shorter, but more importantly consistent with the way you're already doing in the scheduler.server.handlers package.

* main:
  Add sterr to output list (#3649)
  feat: ✨ add Shodan InternetDB boefje (#2615)
  Add search endpoint for schedules for scheduler (#3695)
@stephanie0x00
Copy link
Contributor

Checklist for QA:

  • I have checked out this branch, and successfully ran a fresh make reset.
  • I confirmed that there are no unintended functional regressions in this branch:
    • I have managed to pass the onboarding flow
    • Objects and Findings are created properly
    • Tasks are created and completed properly
  • I confirmed that the PR's advertised feature or hotfix works as intended.
  • I checked the logs for errors and/or warnings and made issues where necessary

What works:

Doesn't seem to break anything. Not really sure if all the error messages are coming through. Double checking this with @jpbruinsslot. In either case this can be merged and if the error messages are not coming through properly we can fix that in additional PRs.

What doesn't work:

n/a

Bug or feature?:

n/a

Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
35.5% Coverage on New Code (required ≥ 80%)

See analysis details on SonarCloud

@underdarknl underdarknl merged commit 4aef1d8 into main Oct 30, 2024
22 of 23 checks passed
@underdarknl underdarknl deleted the chore/mula/api-exception-handling branch October 30, 2024 10:46
jpbruinsslot added a commit that referenced this pull request Nov 6, 2024
* main:
  Fix reports with organization tags (#3790)
  Update README.rst - Fix guidelines URLs (#3789)
  Exclude Report from ooi list (#3768)
  Update `croniter` (#3767)
  Fixes for dropdowns (#3732)
  Fix scheduled Aggregate Report naming (#3748)
  Make systemctl call for kat-rocky-worker conditional (#3782)
  Fix vulnerability chapters in Aggregate table of content (#3780)
  Fix auth token middleware with wrong format header (#3755)
  Add rocky REST API for report recipes (#3746)
  Add exception handling to the rest api (#3708)
  Refactor Multi Report to comply to the new report flow (#3705)
  Fix report names for scheduled reports (#3726)
  Fix Multi Report recursion error (#3714)
  Bump waitress from 3.0.0 to 3.0.1 in /octopoes (#3760)
  Docs/add muted findings (#3699)
  Edit report recipe (#3690)
  Add start date to report schedule (#3701)
  Add REST API to list report and download pdf report (#3689)
  Fixes in Report Overview (#3707)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mula Issues related to the scheduler
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Exception handling in server api of scheduler
4 participants