handling context timeout/cancelled in printer package

This commit is contained in:
Julien Neuhart
2019-07-09 19:39:14 +02:00
parent 957b9b1cf4
commit c7ecdcf625
9 changed files with 81 additions and 61 deletions

View File

@@ -2,10 +2,7 @@ package timeout
import (
"context"
"strings"
"time"
"github.com/thecodingmachine/gotenberg/internal/pkg/standarderror"
)
// Context creates a context with timeout for
@@ -18,26 +15,3 @@ func Context(seconds float64) (context.Context, context.CancelFunc) {
func Duration(seconds float64) time.Duration {
return time.Duration(1000*seconds) * time.Millisecond
}
// Err returns a standarderror.Error
// if the context has an error.
func Err(ctx context.Context) error {
const op = "timeout.Err"
err := ctx.Err()
if err == nil {
return nil
}
if strings.Contains(err.Error(), context.DeadlineExceeded.Error()) {
return &standarderror.Error{
Code: standarderror.Timeout,
Message: "context has timed out",
Op: op,
Err: err,
}
}
return &standarderror.Error{
Message: "context finished with an error",
Op: op,
Err: err,
}
}