mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-14 03:12:14 +01:00
minor refactoring of const + better timeout handling in pinter package + api package tests
This commit is contained in:
@@ -10,11 +10,11 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
defaultWaitTimeoutEnvVar = "DEFAULT_WAIT_TIMEOUT"
|
||||
defaultListenPortEnvVar = "DEFAULT_LISTEN_PORT"
|
||||
disableGoogleChromeEnvVar = "DISABLE_GOOGLE_CHROME"
|
||||
disableUnoconvEnvVar = "DISABLE_UNOCONV"
|
||||
logLevelEnvVar = "LOG_LEVEL"
|
||||
defaultWaitTimeoutEnvVar string = "DEFAULT_WAIT_TIMEOUT"
|
||||
defaultListenPortEnvVar string = "DEFAULT_LISTEN_PORT"
|
||||
disableGoogleChromeEnvVar string = "DISABLE_GOOGLE_CHROME"
|
||||
disableUnoconvEnvVar string = "DISABLE_UNOCONV"
|
||||
logLevelEnvVar string = "LOG_LEVEL"
|
||||
)
|
||||
|
||||
// Config contains the application
|
||||
@@ -40,7 +40,7 @@ func defaultConfig() *Config {
|
||||
// FromEnv fetches configuration
|
||||
// from environment variables.
|
||||
func FromEnv() (*Config, error) {
|
||||
const op = "config.FromEnv"
|
||||
const op string = "config.FromEnv"
|
||||
c := defaultConfig()
|
||||
defaultWaitTimeout, err := defaultWaitTimeoutFromEnv(defaultWaitTimeoutEnvVar, c.DefaultWaitTimeout())
|
||||
c.defaultWaitTimeout = defaultWaitTimeout
|
||||
@@ -103,7 +103,7 @@ func (c *Config) LogLevel() logrus.Level {
|
||||
}
|
||||
|
||||
func defaultWaitTimeoutFromEnv(envVar string, defaultValue float64) (float64, error) {
|
||||
const op = "config.defaultWaitTimeoutFromEnv"
|
||||
const op string = "config.defaultWaitTimeoutFromEnv"
|
||||
if v, ok := os.LookupEnv(envVar); ok {
|
||||
waitTimeout, err := strconv.ParseFloat(v, 64)
|
||||
if err != nil {
|
||||
@@ -119,7 +119,7 @@ func defaultWaitTimeoutFromEnv(envVar string, defaultValue float64) (float64, er
|
||||
}
|
||||
|
||||
func defaultListenPortFromEnv(envVar string, defaultValue string) (string, error) {
|
||||
const op = "config.defaultListenPortFromEnv"
|
||||
const op string = "config.defaultListenPortFromEnv"
|
||||
if v, ok := os.LookupEnv(envVar); ok {
|
||||
portAsUint, err := strconv.ParseUint(v, 10, 64)
|
||||
if err != nil {
|
||||
@@ -142,7 +142,7 @@ func defaultListenPortFromEnv(envVar string, defaultValue string) (string, error
|
||||
}
|
||||
|
||||
func boolFromEnv(envVar string, defaultValue bool) (bool, error) {
|
||||
const op = "config.boolFromEnv"
|
||||
const op string = "config.boolFromEnv"
|
||||
if v, ok := os.LookupEnv(envVar); ok {
|
||||
if v != "1" && v != "0" {
|
||||
return defaultValue, &standarderror.Error{
|
||||
@@ -157,7 +157,7 @@ func boolFromEnv(envVar string, defaultValue bool) (bool, error) {
|
||||
}
|
||||
|
||||
func logLevelFromEnv(envVar string, defaultValue logrus.Level) (logrus.Level, error) {
|
||||
const op = "config.logLevelFromEnv"
|
||||
const op string = "config.logLevelFromEnv"
|
||||
if v, ok := os.LookupEnv(envVar); ok {
|
||||
switch v {
|
||||
case "DEBUG":
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
"github.com/thecodingmachine/gotenberg/internal/pkg/standarderror"
|
||||
)
|
||||
|
||||
const chromeWarmupTime = 10 * time.Second
|
||||
const chromeWarmupTime time.Duration = 10 * time.Second
|
||||
|
||||
type chrome struct {
|
||||
manager *processManager
|
||||
@@ -28,7 +28,7 @@ func (p *chrome) Fullname() string {
|
||||
}
|
||||
|
||||
func (p *chrome) Start() error {
|
||||
const op = "pm2.chrome.Start"
|
||||
const op string = "pm2.chrome.Start"
|
||||
if err := p.manager.start(p); err != nil {
|
||||
return &standarderror.Error{Op: op, Err: err}
|
||||
}
|
||||
@@ -36,7 +36,7 @@ func (p *chrome) Start() error {
|
||||
}
|
||||
|
||||
func (p *chrome) Shutdown() error {
|
||||
const op = "pm2.chrome.Shutdown"
|
||||
const op string = "pm2.chrome.Shutdown"
|
||||
if err := p.manager.shutdown(p); err != nil {
|
||||
return &standarderror.Error{Op: op, Err: err}
|
||||
}
|
||||
@@ -67,7 +67,7 @@ func (p *chrome) name() string {
|
||||
}
|
||||
|
||||
func (p *chrome) viable() bool {
|
||||
const op = "pm2.chrome.viable"
|
||||
const op string = "pm2.chrome.viable"
|
||||
// check if Google Chrome is correctly running.
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
@@ -93,7 +93,7 @@ func (p *chrome) viable() bool {
|
||||
}
|
||||
|
||||
func (p *chrome) warmup() {
|
||||
const op = "pm2.chrome.warmup"
|
||||
const op string = "pm2.chrome.warmup"
|
||||
p.manager.logger.DebugfOp(
|
||||
op,
|
||||
"allowing %v to startup",
|
||||
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
stoppedState = iota
|
||||
stoppedState int32 = iota
|
||||
runningState
|
||||
errorState
|
||||
)
|
||||
@@ -35,7 +35,7 @@ type processManager struct {
|
||||
}
|
||||
|
||||
func (m *processManager) start(p Process) error {
|
||||
const op = "pm2.start"
|
||||
const op string = "pm2.start"
|
||||
if err := m.pm2(p, "start"); err != nil {
|
||||
return &standarderror.Error{Op: op, Err: err}
|
||||
}
|
||||
@@ -63,7 +63,7 @@ func (m *processManager) start(p Process) error {
|
||||
}
|
||||
|
||||
func (m *processManager) shutdown(p Process) error {
|
||||
const op = "pm2.shutdown"
|
||||
const op string = "pm2.shutdown"
|
||||
if m.heuristicState != runningState {
|
||||
return nil
|
||||
}
|
||||
@@ -76,7 +76,7 @@ func (m *processManager) shutdown(p Process) error {
|
||||
}
|
||||
|
||||
func (m *processManager) pm2(p Process, cmdName string) error {
|
||||
const op = "pm2.pm2"
|
||||
const op string = "pm2.pm2"
|
||||
cmdArgs := []string{
|
||||
cmdName,
|
||||
p.name(),
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"github.com/thecodingmachine/gotenberg/internal/pkg/standarderror"
|
||||
)
|
||||
|
||||
const unoconvWarmupTime = 5 * time.Second
|
||||
const unoconvWarmupTime time.Duration = 5 * time.Second
|
||||
|
||||
type unoconv struct {
|
||||
manager *processManager
|
||||
@@ -26,7 +26,7 @@ func (p *unoconv) Fullname() string {
|
||||
}
|
||||
|
||||
func (p *unoconv) Start() error {
|
||||
const op = "pm2.unoconv.Start"
|
||||
const op string = "pm2.unoconv.Start"
|
||||
if err := p.manager.start(p); err != nil {
|
||||
return &standarderror.Error{Op: op, Err: err}
|
||||
}
|
||||
@@ -34,7 +34,7 @@ func (p *unoconv) Start() error {
|
||||
}
|
||||
|
||||
func (p *unoconv) Shutdown() error {
|
||||
const op = "pm2.unoconv.Shutdown"
|
||||
const op string = "pm2.unoconv.Shutdown"
|
||||
if err := p.manager.shutdown(p); err != nil {
|
||||
return &standarderror.Error{Op: op, Err: err}
|
||||
}
|
||||
@@ -60,7 +60,7 @@ func (p *unoconv) viable() bool {
|
||||
}
|
||||
|
||||
func (p *unoconv) warmup() {
|
||||
const op = "pm2.unoconv.warmup"
|
||||
const op string = "pm2.unoconv.warmup"
|
||||
p.manager.logger.DebugfOp(
|
||||
op,
|
||||
"allowing %v to startup",
|
||||
|
||||
@@ -39,83 +39,89 @@ type ChromeOptions struct {
|
||||
}
|
||||
|
||||
func (p *chrome) Print(destination string) error {
|
||||
const op = "printer.chrome.Print"
|
||||
const op string = "printer.chrome.Print"
|
||||
ctx, cancel := timeout.Context(p.opts.WaitTimeout + p.opts.WaitDelay)
|
||||
defer cancel()
|
||||
devt, err := devtool.New("http://localhost:9222").Version(ctx)
|
||||
if err != nil {
|
||||
return handleErrContext(ctx, &standarderror.Error{Op: op, Err: err})
|
||||
resolver := func() error {
|
||||
devt, err := devtool.New("http://localhost:9222").Version(ctx)
|
||||
if err != nil {
|
||||
return &standarderror.Error{Op: op, Err: err}
|
||||
}
|
||||
// connect to WebSocket URL (page) that speaks the Chrome DevTools Protocol.
|
||||
devtConn, err := rpcc.DialContext(ctx, devt.WebSocketDebuggerURL)
|
||||
if err != nil {
|
||||
return &standarderror.Error{Op: op, Err: err}
|
||||
}
|
||||
defer devtConn.Close() // nolint: errcheck
|
||||
// create a new CDP Client that uses conn.
|
||||
devtClient := cdp.NewClient(devtConn)
|
||||
newContextTarget, err := devtClient.Target.CreateBrowserContext(ctx)
|
||||
if err != nil {
|
||||
return &standarderror.Error{Op: op, Err: err}
|
||||
}
|
||||
// create a new blank target with the new browser context.
|
||||
createTargetArgs := target.
|
||||
NewCreateTargetArgs("about:blank").
|
||||
SetBrowserContextID(newContextTarget.BrowserContextID)
|
||||
newTarget, err := devtClient.Target.CreateTarget(ctx, createTargetArgs)
|
||||
if err != nil {
|
||||
return &standarderror.Error{Op: op, Err: err}
|
||||
}
|
||||
// connect the client to the new target.
|
||||
newTargetWsURL := fmt.Sprintf("ws://127.0.0.1:9222/devtools/page/%s", newTarget.TargetID)
|
||||
newContextConn, err := rpcc.DialContext(ctx, newTargetWsURL)
|
||||
if err != nil {
|
||||
return &standarderror.Error{Op: op, Err: err}
|
||||
}
|
||||
defer newContextConn.Close() // nolint: errcheck
|
||||
// create a new CDP Client that uses newContextConn.
|
||||
targetClient := cdp.NewClient(newContextConn)
|
||||
closeTargetArgs := target.NewCloseTargetArgs(newTarget.TargetID)
|
||||
// close the target when done.
|
||||
defer targetClient.Target.CloseTarget(ctx, closeTargetArgs) // nolint: errcheck
|
||||
if err := runBatch(
|
||||
// enable all the domain events that we're interested in.
|
||||
func() error { return targetClient.DOM.Enable(ctx) },
|
||||
func() error { return targetClient.Network.Enable(ctx, network.NewEnableArgs()) },
|
||||
func() error { return targetClient.Page.Enable(ctx) },
|
||||
func() error { return targetClient.Runtime.Enable(ctx) },
|
||||
); err != nil {
|
||||
return &standarderror.Error{Op: op, Err: err}
|
||||
}
|
||||
if err := p.navigate(ctx, targetClient); err != nil {
|
||||
return &standarderror.Error{Op: op, Err: err}
|
||||
}
|
||||
print, err := targetClient.Page.PrintToPDF(
|
||||
ctx,
|
||||
page.NewPrintToPDFArgs().
|
||||
SetPaperWidth(p.opts.PaperWidth).
|
||||
SetPaperHeight(p.opts.PaperHeight).
|
||||
SetMarginTop(p.opts.MarginTop).
|
||||
SetMarginBottom(p.opts.MarginBottom).
|
||||
SetMarginLeft(p.opts.MarginLeft).
|
||||
SetMarginRight(p.opts.MarginRight).
|
||||
SetLandscape(p.opts.Landscape).
|
||||
SetDisplayHeaderFooter(true).
|
||||
SetHeaderTemplate(p.opts.HeaderHTML).
|
||||
SetFooterTemplate(p.opts.FooterHTML).
|
||||
SetPrintBackground(true),
|
||||
)
|
||||
if err != nil {
|
||||
return &standarderror.Error{Op: op, Err: err}
|
||||
}
|
||||
if err := ioutil.WriteFile(destination, print.Data, 0644); err != nil {
|
||||
return &standarderror.Error{Op: op, Err: err}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// connect to WebSocket URL (page) that speaks the Chrome DevTools Protocol.
|
||||
devtConn, err := rpcc.DialContext(ctx, devt.WebSocketDebuggerURL)
|
||||
if err != nil {
|
||||
return handleErrContext(ctx, &standarderror.Error{Op: op, Err: err})
|
||||
}
|
||||
defer devtConn.Close() // nolint: errcheck
|
||||
// create a new CDP Client that uses conn.
|
||||
devtClient := cdp.NewClient(devtConn)
|
||||
newContextTarget, err := devtClient.Target.CreateBrowserContext(ctx)
|
||||
if err != nil {
|
||||
return handleErrContext(ctx, &standarderror.Error{Op: op, Err: err})
|
||||
}
|
||||
// create a new blank target with the new browser context.
|
||||
createTargetArgs := target.
|
||||
NewCreateTargetArgs("about:blank").
|
||||
SetBrowserContextID(newContextTarget.BrowserContextID)
|
||||
newTarget, err := devtClient.Target.CreateTarget(ctx, createTargetArgs)
|
||||
if err != nil {
|
||||
return handleErrContext(ctx, &standarderror.Error{Op: op, Err: err})
|
||||
}
|
||||
// connect the client to the new target.
|
||||
newTargetWsURL := fmt.Sprintf("ws://127.0.0.1:9222/devtools/page/%s", newTarget.TargetID)
|
||||
newContextConn, err := rpcc.DialContext(ctx, newTargetWsURL)
|
||||
if err != nil {
|
||||
return handleErrContext(ctx, &standarderror.Error{Op: op, Err: err})
|
||||
}
|
||||
defer newContextConn.Close() // nolint: errcheck
|
||||
// create a new CDP Client that uses newContextConn.
|
||||
targetClient := cdp.NewClient(newContextConn)
|
||||
closeTargetArgs := target.NewCloseTargetArgs(newTarget.TargetID)
|
||||
// close the target when done.
|
||||
defer targetClient.Target.CloseTarget(ctx, closeTargetArgs) // nolint: errcheck
|
||||
if err := runBatch(
|
||||
// enable all the domain events that we're interested in.
|
||||
func() error { return targetClient.DOM.Enable(ctx) },
|
||||
func() error { return targetClient.Network.Enable(ctx, network.NewEnableArgs()) },
|
||||
func() error { return targetClient.Page.Enable(ctx) },
|
||||
func() error { return targetClient.Runtime.Enable(ctx) },
|
||||
); err != nil {
|
||||
return handleErrContext(ctx, &standarderror.Error{Op: op, Err: err})
|
||||
}
|
||||
if err := p.navigate(ctx, targetClient); err != nil {
|
||||
return handleErrContext(ctx, &standarderror.Error{Op: op, Err: err})
|
||||
}
|
||||
print, err := targetClient.Page.PrintToPDF(
|
||||
ctx,
|
||||
page.NewPrintToPDFArgs().
|
||||
SetPaperWidth(p.opts.PaperWidth).
|
||||
SetPaperHeight(p.opts.PaperHeight).
|
||||
SetMarginTop(p.opts.MarginTop).
|
||||
SetMarginBottom(p.opts.MarginBottom).
|
||||
SetMarginLeft(p.opts.MarginLeft).
|
||||
SetMarginRight(p.opts.MarginRight).
|
||||
SetLandscape(p.opts.Landscape).
|
||||
SetDisplayHeaderFooter(true).
|
||||
SetHeaderTemplate(p.opts.HeaderHTML).
|
||||
SetFooterTemplate(p.opts.FooterHTML).
|
||||
SetPrintBackground(true),
|
||||
)
|
||||
if err != nil {
|
||||
return handleErrContext(ctx, &standarderror.Error{Op: op, Err: err})
|
||||
}
|
||||
if err := ioutil.WriteFile(destination, print.Data, 0644); err != nil {
|
||||
return &standarderror.Error{Op: op, Err: err}
|
||||
if err := resolver(); err != nil {
|
||||
return timeout.Err(ctx, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *chrome) navigate(ctx context.Context, client *cdp.Client) error {
|
||||
const op = "printer.chrome.navigate"
|
||||
const op string = "printer.chrome.navigate"
|
||||
// make sure Page events are enabled.
|
||||
if err := client.Page.Enable(ctx); err != nil {
|
||||
return &standarderror.Error{Op: op, Err: err}
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
|
||||
// NewMarkdown returns a Markdown printer.
|
||||
func NewMarkdown(fpath string, opts *ChromeOptions) (Printer, error) {
|
||||
const op = "printer.NewMarkdown"
|
||||
const op string = "printer.NewMarkdown"
|
||||
tmpl, err := template.
|
||||
New(filepath.Base(fpath)).
|
||||
Funcs(template.FuncMap{"toHTML": markdownToHTML}).
|
||||
@@ -46,7 +46,7 @@ type templateData struct {
|
||||
}
|
||||
|
||||
func markdownToHTML(dirPath, filename string) (template.HTML, error) {
|
||||
const op = "printer.markdownToHTML"
|
||||
const op string = "printer.markdownToHTML"
|
||||
fpath := fmt.Sprintf("%s/%s", dirPath, filename)
|
||||
b, err := ioutil.ReadFile(fpath)
|
||||
if err != nil {
|
||||
|
||||
@@ -29,19 +29,25 @@ func NewMerge(fpaths []string, opts *MergeOptions) Printer {
|
||||
}
|
||||
|
||||
func (p *merge) Print(destination string) error {
|
||||
const op = "printer.merge.Print"
|
||||
const op string = "printer.merge.Print"
|
||||
if p.ctx == nil {
|
||||
ctx, cancel := timeout.Context(p.opts.WaitTimeout)
|
||||
defer cancel()
|
||||
p.ctx = ctx
|
||||
}
|
||||
var cmdArgs []string
|
||||
cmdArgs = append(cmdArgs, p.fpaths...)
|
||||
cmdArgs = append(cmdArgs, "cat", "output", destination)
|
||||
cmd := exec.CommandContext(p.ctx, "pdftk", cmdArgs...)
|
||||
_, err := cmd.Output()
|
||||
if err != nil {
|
||||
return handleErrContext(p.ctx, &standarderror.Error{Op: op, Err: err})
|
||||
resolver := func() error {
|
||||
var cmdArgs []string
|
||||
cmdArgs = append(cmdArgs, p.fpaths...)
|
||||
cmdArgs = append(cmdArgs, "cat", "output", destination)
|
||||
cmd := exec.CommandContext(p.ctx, "pdftk", cmdArgs...)
|
||||
_, err := cmd.Output()
|
||||
if err != nil {
|
||||
return &standarderror.Error{Op: op, Err: err}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if err := resolver(); err != nil {
|
||||
return timeout.Err(p.ctx, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -34,18 +34,24 @@ func NewOffice(fpaths []string, opts *OfficeOptions) Printer {
|
||||
}
|
||||
|
||||
func (p *office) Print(destination string) error {
|
||||
const op = "printer.office.Print"
|
||||
const op string = "printer.office.Print"
|
||||
ctx, cancel := timeout.Context(p.opts.WaitTimeout)
|
||||
defer cancel()
|
||||
fpaths := make([]string, len(p.fpaths))
|
||||
dirPath := filepath.Dir(destination)
|
||||
for i, fpath := range p.fpaths {
|
||||
baseFilename := random.String(32)
|
||||
tmpDest := fmt.Sprintf("%s/%d%s.pdf", dirPath, i, baseFilename)
|
||||
if err := unoconv(ctx, fpath, tmpDest, p.opts); err != nil {
|
||||
return handleErrContext(ctx, &standarderror.Error{Op: op, Err: err})
|
||||
resolver := func() error {
|
||||
dirPath := filepath.Dir(destination)
|
||||
for i, fpath := range p.fpaths {
|
||||
baseFilename := random.String(32)
|
||||
tmpDest := fmt.Sprintf("%s/%d%s.pdf", dirPath, i, baseFilename)
|
||||
if err := unoconv(ctx, fpath, tmpDest, p.opts); err != nil {
|
||||
return &standarderror.Error{Op: op, Err: err}
|
||||
}
|
||||
fpaths[i] = tmpDest
|
||||
}
|
||||
fpaths[i] = tmpDest
|
||||
return nil
|
||||
}
|
||||
if err := resolver(); err != nil {
|
||||
return timeout.Err(ctx, err)
|
||||
}
|
||||
if len(fpaths) == 1 {
|
||||
if err := os.Rename(fpaths[0], destination); err != nil {
|
||||
@@ -67,7 +73,7 @@ func (p *office) Print(destination string) error {
|
||||
var mu sync.Mutex
|
||||
|
||||
func unoconv(ctx context.Context, fpath, destination string, opts *OfficeOptions) error {
|
||||
const op = "printer.unoconv"
|
||||
const op string = "printer.unoconv"
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
cmdArgs := []string{
|
||||
|
||||
@@ -1,39 +1,7 @@
|
||||
package printer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/thecodingmachine/gotenberg/internal/pkg/standarderror"
|
||||
)
|
||||
|
||||
// Printer is a type that can create a PDF file from a source.
|
||||
// The source is defined in the underlying implementation.
|
||||
type Printer interface {
|
||||
Print(destination string) error
|
||||
}
|
||||
|
||||
func handleErrContext(ctx context.Context, previousErr error) error {
|
||||
const op = "printer.handleErrContext"
|
||||
if previousErr == nil {
|
||||
panic(fmt.Sprintf("%s: previous error should not be nil", op))
|
||||
}
|
||||
err := ctx.Err()
|
||||
if err == nil {
|
||||
return previousErr
|
||||
}
|
||||
if strings.Contains(err.Error(), context.DeadlineExceeded.Error()) {
|
||||
return &standarderror.Error{
|
||||
Code: standarderror.Timeout,
|
||||
Message: "context has timed out",
|
||||
Op: op,
|
||||
Err: previousErr,
|
||||
}
|
||||
}
|
||||
return &standarderror.Error{
|
||||
Message: "context finished with an error",
|
||||
Op: op,
|
||||
Err: previousErr,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
package printer
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/thecodingmachine/gotenberg/internal/pkg/standarderror"
|
||||
"github.com/thecodingmachine/gotenberg/internal/pkg/timeout"
|
||||
"github.com/thecodingmachine/gotenberg/test"
|
||||
)
|
||||
|
||||
func TestHandlerErr(t *testing.T) {
|
||||
previousErr := errors.New("previous error")
|
||||
// should be OK.
|
||||
ctx, cancel := timeout.Context(5)
|
||||
defer cancel()
|
||||
assert.NotNil(t, handleErrContext(ctx, previousErr))
|
||||
// should timeout.
|
||||
ctx, cancel = timeout.Context(0.5)
|
||||
defer cancel()
|
||||
time.Sleep(timeout.Duration(1))
|
||||
err := handleErrContext(ctx, previousErr)
|
||||
assert.NotNil(t, err)
|
||||
standardized := test.RequireStandardError(t, err)
|
||||
assert.Equal(t, standarderror.Timeout, standardized.Code)
|
||||
// should failed.
|
||||
ctx, cancel = timeout.Context(5)
|
||||
cancel()
|
||||
err = handleErrContext(ctx, previousErr)
|
||||
assert.NotNil(t, err)
|
||||
standardized = test.RequireStandardError(t, err)
|
||||
assert.Equal(t, standarderror.Internal, standarderror.Code(err))
|
||||
}
|
||||
@@ -64,7 +64,7 @@ func Code(err error) string {
|
||||
return Internal
|
||||
}
|
||||
|
||||
const defaultMessage = "an internal error has occurred: please contact technical support"
|
||||
const defaultMessage string = "an internal error has occurred: please contact technical support"
|
||||
|
||||
// Message returns the human-readable message of the error, if available.
|
||||
// Otherwise returns a generic error message.
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
// Package timeout helps creating
|
||||
// Package timeout helps managing
|
||||
// context with timeout.
|
||||
package timeout
|
||||
|
||||
@@ -2,7 +2,11 @@ package timeout
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/thecodingmachine/gotenberg/internal/pkg/standarderror"
|
||||
)
|
||||
|
||||
// Context creates a context with timeout for
|
||||
@@ -15,3 +19,29 @@ func Context(seconds float64) (context.Context, context.CancelFunc) {
|
||||
func Duration(seconds float64) time.Duration {
|
||||
return time.Duration(1000*seconds) * time.Millisecond
|
||||
}
|
||||
|
||||
// Err checks if there is an error in the given context
|
||||
// and wraps the previous error inside a standarderror.Error.
|
||||
func Err(ctx context.Context, previousErr error) error {
|
||||
const op string = "timeout.Err"
|
||||
if previousErr == nil {
|
||||
panic(fmt.Sprintf("%s: previous error should not be nil", op))
|
||||
}
|
||||
err := ctx.Err()
|
||||
if err == nil {
|
||||
return previousErr
|
||||
}
|
||||
if strings.Contains(err.Error(), context.DeadlineExceeded.Error()) {
|
||||
return &standarderror.Error{
|
||||
Code: standarderror.Timeout,
|
||||
Message: "context has timed out",
|
||||
Op: op,
|
||||
Err: previousErr,
|
||||
}
|
||||
}
|
||||
return &standarderror.Error{
|
||||
Message: "context finished with an error",
|
||||
Op: op,
|
||||
Err: previousErr,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +1,40 @@
|
||||
package timeout
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/thecodingmachine/gotenberg/internal/pkg/standarderror"
|
||||
"github.com/thecodingmachine/gotenberg/test"
|
||||
)
|
||||
|
||||
func TestContext(t *testing.T) {
|
||||
ctx, cancel := Context(1.5)
|
||||
assert.NotNil(t, ctx)
|
||||
assert.NotNil(t, cancel)
|
||||
}
|
||||
|
||||
func TestDuration(t *testing.T) {
|
||||
expected := time.Duration(1500) * time.Millisecond
|
||||
result := Duration(1.5)
|
||||
assert.Equal(t, expected.String(), result.String())
|
||||
}
|
||||
|
||||
func TestErr(t *testing.T) {
|
||||
previousErr := errors.New("previous error")
|
||||
// should be OK.
|
||||
ctx, cancel := Context(5)
|
||||
defer cancel()
|
||||
assert.NotNil(t, Err(ctx, previousErr))
|
||||
// should timeout.
|
||||
ctx, cancel = Context(0.5)
|
||||
defer cancel()
|
||||
time.Sleep(Duration(1))
|
||||
err := Err(ctx, previousErr)
|
||||
assert.NotNil(t, err)
|
||||
standardized := test.RequireStandardError(t, err)
|
||||
assert.Equal(t, standarderror.Timeout, standardized.Code)
|
||||
// should failed.
|
||||
ctx, cancel = Context(5)
|
||||
cancel()
|
||||
err = Err(ctx, previousErr)
|
||||
assert.NotNil(t, err)
|
||||
standardized = test.RequireStandardError(t, err)
|
||||
assert.Equal(t, standarderror.Internal, standarderror.Code(err))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user