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

refactor(i): Lint casts & move panics under must funcs #3134

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

shahzadlone
Copy link
Member

Relevant issue(s)

Resolves #3103

Description

  • Fix the BadRequest in the acp handlers to now be panics
  • Make the panics documented under must functions
  • Add the linter to avoid increasing of undocumented panics
  • Ignore linter in existing files outside cli and http (this should either be resolved gradually over time or in a single PR, regardless would prefer to not block this PR because of them)

@shahzadlone shahzadlone added the code quality Related to improving code quality label Oct 15, 2024
@shahzadlone shahzadlone added this to the DefraDB v0.14 milestone Oct 15, 2024
@shahzadlone shahzadlone requested a review from a team October 15, 2024 14:22
@shahzadlone shahzadlone self-assigned this Oct 15, 2024
@shahzadlone shahzadlone changed the title refac(i): Panic in handler over error, & lint panic casts refac(i): Panic in handler over error & lint panic casts Oct 15, 2024
@shahzadlone shahzadlone force-pushed the lone/fix/bad-request-to-internal-server-error branch from 1bcee1c to 1b8b14e Compare October 15, 2024 14:22
@shahzadlone shahzadlone changed the title refac(i): Panic in handler over error & lint panic casts refac(i): Panic in handler & lint panic casts Oct 15, 2024
Copy link

codecov bot commented Oct 15, 2024

Codecov Report

Attention: Patch coverage is 75.82418% with 22 lines in your changes missing coverage. Please review.

Project coverage is 77.79%. Comparing base (47d38a8) to head (ec7e43d).

Files with missing lines Patch % Lines
http/handler_tx.go 56.52% 8 Missing and 2 partials ⚠️
http/handler.go 33.33% 3 Missing and 1 partial ⚠️
http/handler_lens.go 0.00% 4 Missing ⚠️
http/handler_collection.go 80.00% 2 Missing ⚠️
http/handler_store.go 85.71% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #3134      +/-   ##
===========================================
- Coverage    78.10%   77.79%   -0.31%     
===========================================
  Files          354      354              
  Lines        34660    34678      +18     
===========================================
- Hits         27071    26977      -94     
- Misses        5972     6064      +92     
- Partials      1617     1637      +20     
Flag Coverage Δ
all-tests 77.79% <75.82%> (-0.31%) ⬇️

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

Files with missing lines Coverage Δ
cli/p2p_info.go 100.00% <100.00%> (ø)
cli/purge.go 93.33% <100.00%> (ø)
cli/utils.go 70.27% <100.00%> (+0.55%) ⬆️
http/errors.go 28.00% <ø> (ø)
http/handler_acp.go 91.18% <100.00%> (+7.73%) ⬆️
http/handler_ccip.go 95.12% <100.00%> (ø)
http/handler_extras.go 100.00% <100.00%> (ø)
http/handler_p2p.go 73.21% <100.00%> (ø)
http/middleware.go 70.00% <100.00%> (ø)
http/utils.go 88.00% <100.00%> (+9.43%) ⬆️
... and 5 more

... and 15 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 47d38a8...ec7e43d. Read the comment docs.

Copy link
Contributor

@AndrewSisley AndrewSisley left a comment

Choose a reason for hiding this comment

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

todo: This appears to be entirely untested, given the lack of any changes made to test files, and no note of any manual testing in the PR description. I prefer that you add tests, but if you really think manual testing is okay in the short term, please note why.

I also have a question RE the linter excluded file list.

@shahzadlone
Copy link
Member Author

todo: This appears to be entirely untested, given the lack of any changes made to test files, and no note of any manual testing in the PR description. I prefer that you add tests, but if you really think manual testing is okay in the short term, please note why.

Happy to add tests if you have any specific code blocks that I should test, here are all the changes that have been done:

  • Implicit casts that would panic .() moved to mustXYZ functions -> Won't change or need tests as it's the same behavior.
  • HTTP handler functions changed to follow the same practice as all other existing handlers to panic instead of returning 400.
  • Added extra linter (testing not applicable)
  • Some instances of double casts just split into multiple must/``try` checks.

@AndrewSisley
Copy link
Contributor

HTTP handler functions changed to follow the same practice as all other existing handlers to panic instead of returning 400.

It was this I was mostly after - might not be worth the hassle of writing tests for though as I guess it is mostly just the http status code that changed, and that's not something the integration test framework can handle atm I think. Have you manually tested this?

responseJSON(rw, http.StatusBadRequest, errorResponse{NewErrFailedToGetContext("db")})
return
}
db := mustGetContextClientDB(req)
Copy link
Collaborator

Choose a reason for hiding this comment

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

question: Why is this turned into a must instead of a try like what you did is some other handles?

Copy link
Member Author

Choose a reason for hiding this comment

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

Only one that remained a try (not panic) is the P2P one because I think in that case it makes sense because P2P might be turned off and I see that specific error being returned by original author of the p2p code as useful for that situation as useful.

Rest all context casts are converted to a must.

http/middleware.go Outdated Show resolved Hide resolved

dsTxn, ok := txVal.(datastore.Txn)
if !ok {
responseJSON(rw, http.StatusBadRequest, errorResponse{ErrInvalidDataStoreTransaction})
Copy link
Collaborator

Choose a reason for hiding this comment

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

todo: This isn't a BadRequest but rather an InternalServerError (500).

Out of scope of this PR but a lot of our returned statuses are wrong like this. I create #3139 to track this. If a panic within the handlers automatically returns a 500 status then it might be better to just do a must typecast and let the server panic for cases where a 500 is appropriate.

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually #3139 seems similar / duplicate of what I wanted #3103 to be originally (if i understand correctly) . So I would say that is very much in the scope of this PR. I can change all the required BadRequests that need changing if you can guide me to which additional ones should be changed (@nasdf suggested only for the context gets+casts originally), I can take care of them in this PR.

@shahzadlone
Copy link
Member Author

HTTP handler functions changed to follow the same practice as all other existing handlers to panic instead of returning 400.

It was this I was mostly after - might not be worth the hassle of writing tests for though as I guess it is mostly just the http status code that changed, and that's not something the integration test framework can handle atm I think. Have you manually tested this?

Manually some yes, but not all. I am a bit unsure how to test certain situations.

@shahzadlone shahzadlone force-pushed the lone/fix/bad-request-to-internal-server-error branch from 19c5438 to 77bdcdb Compare October 17, 2024 16:45
@shahzadlone shahzadlone changed the title refac(i): Panic in handler & lint panic casts refac(i): Lint panic casts & mustify Oct 17, 2024
@shahzadlone shahzadlone changed the title refac(i): Lint panic casts & mustify refac(i): Lint panic casts & mustify panics Oct 17, 2024
@shahzadlone shahzadlone changed the title refac(i): Lint panic casts & mustify panics refac(i): Lint check casts & mustify panic casts Oct 17, 2024
@shahzadlone shahzadlone force-pushed the lone/fix/bad-request-to-internal-server-error branch from 77bdcdb to 43ade30 Compare October 17, 2024 16:57
@shahzadlone shahzadlone changed the title refac(i): Lint check casts & mustify panic casts refac(i): Lint and mustify panic casts Oct 17, 2024
@shahzadlone shahzadlone changed the title refac(i): Lint and mustify panic casts refac(i): Lint and move panic casts under must Oct 17, 2024
@shahzadlone shahzadlone changed the title refac(i): Lint and move panic casts under must refac(i): Lint & move panic casts under must Oct 17, 2024
@shahzadlone shahzadlone changed the title refac(i): Lint & move panic casts under must refac(i): Move panic casts under must funcs Oct 17, 2024
@shahzadlone shahzadlone changed the title refac(i): Move panic casts under must funcs refactor(i): Move panic casts under must funcs Oct 17, 2024
@shahzadlone shahzadlone changed the title refactor(i): Move panic casts under must funcs refactor(i): Move & lint panic casts under must funcs Oct 19, 2024
@shahzadlone shahzadlone changed the title refactor(i): Move & lint panic casts under must funcs refactor(i): Lint & Move panic casts under must funcs Oct 19, 2024
@shahzadlone shahzadlone changed the title refactor(i): Lint & Move panic casts under must funcs refactor(i): Lint & move panic casts under must funcs Oct 19, 2024
@shahzadlone shahzadlone requested a review from a team October 20, 2024 13:11
@shahzadlone shahzadlone force-pushed the lone/fix/bad-request-to-internal-server-error branch from 43ade30 to ec7e43d Compare October 20, 2024 13:25
@shahzadlone shahzadlone changed the title refactor(i): Lint & move panic casts under must funcs refactor(i): Lint casts & move panics under must funcs Oct 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code quality Related to improving code quality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix StatusBadRequest -> StatusInternalServerError for internal errors in http handler
3 participants