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

context: added panic recovery to forked contexts #770

Merged
merged 4 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@

# Dependency directories (remove the comment below to include it)
# vendor/
.idea/
ei09010 marked this conversation as resolved.
Show resolved Hide resolved
38 changes: 28 additions & 10 deletions lib/middleware/request/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package request

import (
"context"
"fmt"
"time"

"github.com/cuvva/cuvva-public-go/lib/clog"
Expand Down Expand Up @@ -40,12 +41,19 @@ type ContextKey string
// ForkContext provides a callback function with a new context inheriting values from the request context, and will log any error returned by the callback
func ForkContext(ctx context.Context, fn func(context.Context) error) {
newCtx := cloneContext(ctx)

go func() {
err := fn(newCtx)
if err != nil {
clog.Get(newCtx).WithError(err).Log(clog.DetermineLevel(err, true), "forked context errored")
}
var err error
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic: %v", r)
}

if err != nil {
clog.Get(ctx).WithError(err).Log(clog.DetermineLevel(err, true), "forked context errored")
}
}()

err = fn(newCtx)
}()
}

Expand All @@ -54,10 +62,20 @@ func ForkContextWithTimeout(ctx context.Context, timeout time.Duration, fn func(
newCtx, cancel := context.WithTimeout(cloneContext(ctx), timeout)

go func() {
defer cancel()
err := fn(newCtx)
if err != nil {
clog.Get(newCtx).WithError(err).Log(clog.DetermineLevel(err, true), "forked context errored")
}
var err error
defer func() {
cancel()
r := recover()
if r != nil {
err = fmt.Errorf("panic: %v", r)
}

if err != nil {
clog.Get(ctx).WithError(err).Log(clog.DetermineLevel(err, true), "forked context errored")
}
}()

err = fn(newCtx)
}()

}
4 changes: 2 additions & 2 deletions lib/restbase/content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ func TestEncode(t *testing.T) {
MIMEType string
Error error
}{
{"DefaultEncodingJSON", "foo", http.StatusOK, []byte(`"foo"` + "\n"), "application/json; charset=utf-8", nil},
{"NoAccept", "foo", http.StatusOK, []byte(`"foo"` + "\n"), "application/json; charset=utf-8", nil},
{"DefaultEncodingJSON", "test", http.StatusOK, []byte(`"test"` + "\n"), "application/json; charset=utf-8", nil},
ei09010 marked this conversation as resolved.
Show resolved Hide resolved
{"NoAccept", "test", http.StatusOK, []byte(`"test"` + "\n"), "application/json; charset=utf-8", nil},
}

for _, test := range tests {
Expand Down
Loading