-
Notifications
You must be signed in to change notification settings - Fork 5
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
feat: [#521] User can custom recover when a request panic #132
Conversation
Important Review skippedAuto reviews are limited to specific labels. 🏷️ Labels to auto review (1)
Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the 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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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 using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #132 +/- ##
==========================================
- Coverage 79.09% 76.89% -2.21%
==========================================
Files 10 13 +3
Lines 885 1177 +292
==========================================
+ Hits 700 905 +205
- Misses 159 229 +70
- Partials 26 43 +17 ☔ View full report in Codecov by Sentry. |
I don't understand the reason for the error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, LGTM, please optimize test cases.
route.go
Outdated
@@ -14,7 +14,7 @@ import ( | |||
|
|||
"github.com/gofiber/fiber/v2" | |||
"github.com/gofiber/fiber/v2/middleware/logger" | |||
"github.com/gofiber/fiber/v2/middleware/recover" | |||
fiberRecover "github.com/gofiber/fiber/v2/middleware/recover" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fiberRecover "github.com/gofiber/fiber/v2/middleware/recover" | |
fiberrecover "github.com/gofiber/fiber/v2/middleware/recover" |
middleware_timeout.go
Outdated
ctx.Request().Next() | ||
close(done) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The CI error is caused by this line, basically, the process will be blocked when moving this line here, gin has this issue as well, please create another PR to fix this: Keep this line in defer.
@@ -22,21 +22,14 @@ func Timeout(timeout time.Duration) contractshttp.Middleware { | |||
done := make(chan struct{}) | |||
|
|||
go func() { | |||
defer func() { | |||
if r := recover(); r != nil { | |||
LogFacade.Request(ctx.Request()).Error(r) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The log is required, please keep it. I saw it was removed in gin as well, please add it back.
Created a PR in gin to fix the issue: goravel/gin#122, you can implement the same logic in fiber. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pretty good, a few nitpicks.
middleware_timeout_test.go
Outdated
|
||
mockLog.AssertExpectations(t) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can be removed since we are using mockslog.NewLog(t)
.
mockLog.AssertExpectations(t) |
route.go
Outdated
} else { | ||
ctx.Request().AbortWithStatusJson(http.StatusInternalServerError, fiber.Map{"error": "Internal Server Error"}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The callback should not be nil
here, so we can:
} else { | |
ctx.Request().AbortWithStatusJson(http.StatusInternalServerError, fiber.Map{"error": "Internal Server Error"}) |
route_test.go
Outdated
|
||
mockConfig.AssertExpectations(t) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mockConfig.AssertExpectations(t) |
route_test.go
Outdated
|
||
route, err := NewRoute(mockConfig, nil) | ||
assert.Nil(t, err) | ||
route.Recover(globalRecoverCallback) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is unnecessary in this case, we don't need to pass nil
into the Recover
method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in goravel/gin we used gin.Recovery() and the default recovery test covered this case. Is it necessary to test the default recoverer in goravel/fiber?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can initially set globalRecoverCallback
globalRecoverCallback := func(ctx contractshttp.Context, err any) { ctx.Request().AbortWithStatusJson(http.StatusInternalServerError, fiber.Map{"error": "Internal Server Error"}) }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is default recovery as well:
fiberHandlers := []fiber.Handler{
fiberrecover.New(fiberrecover.Config{
EnableStackTrace: debug,
}),
}
Setting a default globalRecoverCallback
is a good idea.
route_test.go
Outdated
|
||
mockConfig.AssertExpectations(t) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mockConfig.AssertExpectations(t) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
📑 Description
Closes goravel/goravel#521
@coderabbitai summary
✅ Checks