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

fix(#979): GetHubFromContext may crash instead of returning nil #980

Merged

Conversation

kohnalex
Copy link
Contributor

@kohnalex kohnalex commented Feb 22, 2024

fix #979

Summary by CodeRabbit

  • Refactor
    • Improved error handling and clarity in hub retrieval from context by splitting the functionality into two distinct methods.
  • Documentation
    • Updated fibersentry package README with new usage instructions emphasizing *sentry.Hub instance access methods.
  • Tests
    • Restructured test cases in sentry_test.go to cover various middleware states for handling panics and capturing events.

@github-actions github-actions bot added the ☢️ Bug Something isn't working label Feb 22, 2024
@kohnalex
Copy link
Contributor Author

kohnalex commented Feb 23, 2024

Thanks for approving! I have no write access, so a merge would be nice.

@ReneWerner87
Copy link
Member

@kohnalex pls add description to the readme

Copy link

coderabbitai bot commented Mar 12, 2024

Walkthrough

The recent update in the fibersentry/sentry.go file addresses a critical issue by refining the error handling mechanism for the GetHubFromContext function. By splitting it into MustGetHubFromContext and GetHubFromContext, the changes ensure a safer and more predictable way to retrieve the Sentry hub from the context, particularly when the necessary middleware is not registered.

Changes

File Change Summary
fibersentry/sentry.go Split GetHubFromContext into MustGetHubFromContext and GetHubFromContext for better error handling.
fibersentry/README.md Updated fibersentry package usage instructions to reflect new method usage for accessing *sentry.Hub.
fibersentry/sentry_test.go Restructured test cases, added testCase structs, and expanded test scenarios for middleware states.

Assessment against linked issues

Objective Addressed Explanation
Fix runtime panic in GetHubFromContext() when middleware not registered (#979)
Prevent application crash due to faulty interface conversion without middleware (#979)
Ensure GetHubFromContext() returns nil gracefully without middleware (#979)
Align behavior with readme for explicit nil check handling (#979)

Poem

In the code where bugs did roam,
A rabbit hopped, making it home.
With a hop, a split, a clever fix,
No more panics, no more tricks.
🌟 In the land of sentry.go,
The code now safe, the errors slow.
"Hooray!" the developers cried,
For the rabbit's wisdom, far and wide.

🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@gaby
Copy link
Member

gaby commented Mar 12, 2024

@kohnalex Can you update the README with the new function. Also add a unit-test for it. Thanks!

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between b1f09b6 and 782a636.
Files selected for processing (1)
  • fibersentry/sentry.go (2 hunks)
Additional comments: 2
fibersentry/sentry.go (2)
  • 55-55: The MustGetHubFromContext function correctly implements the requested behavior to panic if the Sentry hub is not present in the context. Ensure this function is used in contexts where the presence of the Sentry hub is guaranteed to avoid unintended panics.
  • 59-64: The GetHubFromContext function now safely checks for the presence of the Sentry hub in the context and returns nil if it's absent. This change effectively addresses the issue of runtime panics when the Sentry middleware is not registered. Ensure that the nil return value is appropriately handled wherever this function is used.

@ReneWerner87
Copy link
Member

ReneWerner87 commented Mar 24, 2024

@kohnalex friendly ping, pls check the last hints

  • Add description in readme
  • Add unittests for the new/adjusted code

@kohnalex kohnalex requested a review from a team as a code owner March 26, 2024 16:22
@kohnalex kohnalex requested review from gaby, sixcolors and efectn and removed request for a team March 26, 2024 16:22
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 782a636 and 34ef676.
Files selected for processing (2)
  • fibersentry/README.md (1 hunks)
  • fibersentry/sentry_test.go (2 hunks)
Additional comments: 5
fibersentry/README.md (1)
  • 44-44: The documentation mentions the use of fibersentry.GetHubFromContext() and fibersentry.MustGetHubFromContext() methods. It's crucial to clearly explain the difference between these two methods, especially the conditions under which one would panic and the other would return nil. This clarification will help users make informed decisions about which method to use based on their error handling strategy.
fibersentry/sentry_test.go (4)
  • 15-22: The introduction of the testCase struct is a good practice as it organizes test parameters and expected outcomes clearly. This approach enhances readability and maintainability of the test code. However, ensure that all relevant fields are included in the struct to cover various test scenarios comprehensively.
  • 24-55: The testCasesBeforeRegister function demonstrates how to test the behavior of MustGetHubFromContext and GetHubFromContext before the Sentry middleware is registered. This is crucial for ensuring that the system behaves as expected in scenarios where the middleware might not be present. It's a good practice to cover such edge cases in tests to prevent runtime errors.
  • 57-178: The testCasesAfterRegister variable contains test cases that simulate various scenarios after the Sentry middleware has been registered. This includes testing panic handling, POST requests with different body sizes, and ensuring that Sentry events are captured as expected. It's important to also test the behavior when the Sentry middleware is correctly in place to verify that it captures and reports errors as intended. Consider adding more test cases if there are other significant paths or behaviors that need to be verified.
  • 214-229: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [180-226]

The Test_Sentry function effectively iterates over the test cases defined before and after the Sentry middleware registration, running each one in a subtest. This structure is beneficial for isolating each test case and providing clear feedback on which specific test fails, if any. Ensure that the Sentry client is correctly initialized and that the test environment is properly set up before running these tests to avoid false negatives.

@kohnalex
Copy link
Contributor Author

Sorry for the wait... I had to restructure the test cases a little so app.Use(New()) will be called after the test cases I have added.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 34ef676 and dc645fc.
Files selected for processing (1)
  • fibersentry/README.md (1 hunks)
Additional comments: 1
fibersentry/README.md (1)
  • 44-45: The documentation update clearly explains the usage of fibersentry.GetHubFromContext() and fibersentry.MustGetHubFromContext(), aligning with the PR's objectives to enhance error handling and prevent runtime panics when Sentry middleware is not registered. However, it would be beneficial to include an example or a brief explanation of the scenarios where one might prefer GetHubFromContext() over MustGetHubFromContext() and vice versa, to provide clearer guidance to the users.

fibersentry/README.md Outdated Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between dc645fc and 1430e7e.
Files selected for processing (1)
  • fibersentry/README.md (1 hunks)
Additional comments: 1
fibersentry/README.md (1)
  • 47-47: The note about middleware order is crucial for avoiding runtime errors. However, consider rephrasing for clarity and adding a comma after the introductory phrase for better readability. The current phrasing is already clear, but always aim for the highest clarity in documentation.

fibersentry/README.md Show resolved Hide resolved
@xEricL
Copy link

xEricL commented Apr 12, 2024

What else is needed to merge this?

@ReneWerner87 ReneWerner87 merged commit 22e51ae into gofiber:main Apr 25, 2024
59 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
☢️ Bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐛 [Bug]: FiberSentry: GetHubFromContext() never returns nil
5 participants