mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-14 19:32:15 +01:00
huge refactoring
This commit is contained in:
3
internal/pkg/xcontext/doc.go
Normal file
3
internal/pkg/xcontext/doc.go
Normal file
@@ -0,0 +1,3 @@
|
||||
// Package xcontext helps managing
|
||||
// context.Context with timeout.
|
||||
package xcontext
|
||||
56
internal/pkg/xcontext/xcontext.go
Normal file
56
internal/pkg/xcontext/xcontext.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package xcontext
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/thecodingmachine/gotenberg/internal/pkg/xerror"
|
||||
"github.com/thecodingmachine/gotenberg/internal/pkg/xlog"
|
||||
"github.com/thecodingmachine/gotenberg/internal/pkg/xtime"
|
||||
)
|
||||
|
||||
// WithTimeout creates a context.Context which
|
||||
// times out after given seconds.
|
||||
func WithTimeout(logger xlog.Logger, seconds float64) (context.Context, context.CancelFunc) {
|
||||
const op string = "xcontext.WithTimeout"
|
||||
logger.DebugfOp(op, "creating context with '%.2fs' of timeout...", seconds)
|
||||
return context.WithTimeout(context.Background(), xtime.Duration(seconds))
|
||||
}
|
||||
|
||||
/*
|
||||
MustHandleError checks if there is an error
|
||||
in the given Context.
|
||||
|
||||
If no error, returns the previous error.
|
||||
|
||||
If context.DeadlineExceeded, wraps the previous
|
||||
error inside an xerror.Error with xerror.TimeoutCode.
|
||||
|
||||
Otherwise wraps the previous error inside an
|
||||
xerror.Error.
|
||||
|
||||
It panics if no previous error.
|
||||
*/
|
||||
func MustHandleError(ctx context.Context, previousErr error) error {
|
||||
const op string = "xcontext.MustHandleError"
|
||||
if previousErr == nil {
|
||||
panic(fmt.Sprintf("%s: previous error should not be nil", op))
|
||||
}
|
||||
err := ctx.Err()
|
||||
if err == nil {
|
||||
// we do not wrap the previous error
|
||||
// as it should be wrapped by the caller.
|
||||
return previousErr
|
||||
}
|
||||
// context has timed out
|
||||
if strings.Contains(err.Error(), context.DeadlineExceeded.Error()) {
|
||||
return xerror.Timeout(op, "context has timed out", previousErr)
|
||||
}
|
||||
/*
|
||||
context has another error: we do not
|
||||
wrap the error from the Context as the previous
|
||||
error should contain it.
|
||||
*/
|
||||
return xerror.New(op, previousErr)
|
||||
}
|
||||
43
internal/pkg/xcontext/xcontext_test.go
Normal file
43
internal/pkg/xcontext/xcontext_test.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package xcontext
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/thecodingmachine/gotenberg/internal/pkg/xerror"
|
||||
"github.com/thecodingmachine/gotenberg/internal/pkg/xtime"
|
||||
"github.com/thecodingmachine/gotenberg/test/internalpkg/xerrortest"
|
||||
"github.com/thecodingmachine/gotenberg/test/internalpkg/xlogtest"
|
||||
)
|
||||
|
||||
func TestMustHandleError(t *testing.T) {
|
||||
previousErr := errors.New("previous error")
|
||||
logger := xlogtest.DebugLogger()
|
||||
// context should not have an error.
|
||||
ctx, cancel := WithTimeout(logger, 5)
|
||||
defer cancel()
|
||||
err := MustHandleError(ctx, previousErr)
|
||||
assert.Equal(t, previousErr, err)
|
||||
// should panic.
|
||||
ctx, cancel = WithTimeout(logger, 5)
|
||||
defer cancel()
|
||||
assert.Panics(t, func() {
|
||||
MustHandleError(ctx, nil)
|
||||
})
|
||||
// context should timed out.
|
||||
ctx, cancel = WithTimeout(logger, 0.5)
|
||||
defer cancel()
|
||||
time.Sleep(xtime.Duration(1))
|
||||
err = MustHandleError(ctx, previousErr)
|
||||
xerr := xerrortest.AssertError(t, err)
|
||||
assert.Equal(t, xerror.TimeoutCode, xerror.Code(xerr))
|
||||
// context should have an error different
|
||||
// than context.DeadlineExceeded.
|
||||
ctx, cancel = WithTimeout(logger, 5)
|
||||
cancel()
|
||||
err = MustHandleError(ctx, previousErr)
|
||||
xerr = xerrortest.AssertError(t, err)
|
||||
assert.Equal(t, xerror.InternalCode, xerror.Code(xerr))
|
||||
}
|
||||
Reference in New Issue
Block a user