huge refactoring

This commit is contained in:
Julien Neuhart
2019-07-23 13:57:18 +02:00
parent f6b357691c
commit 7bfbda4490
105 changed files with 4051 additions and 2667 deletions

View File

@@ -0,0 +1,3 @@
// Package xcontext helps managing
// context.Context with timeout.
package xcontext

View 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)
}

View 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))
}