From 186c09f29bd8b32ec2332f130716bd64485d8f20 Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Mon, 8 Jul 2019 11:16:47 +0200 Subject: [PATCH] adding op and standarderror in printer package --- internal/pkg/printer/chrome.go | 37 ++++++++++++++++++-------------- internal/pkg/printer/markdown.go | 11 ++++++---- internal/pkg/printer/merge.go | 7 ++++-- internal/pkg/printer/office.go | 18 ++++++++++++---- 4 files changed, 47 insertions(+), 26 deletions(-) diff --git a/internal/pkg/printer/chrome.go b/internal/pkg/printer/chrome.go index 82f05569..3e1a483d 100644 --- a/internal/pkg/printer/chrome.go +++ b/internal/pkg/printer/chrome.go @@ -12,6 +12,7 @@ import ( "github.com/mafredri/cdp/protocol/page" "github.com/mafredri/cdp/protocol/target" "github.com/mafredri/cdp/rpcc" + "github.com/thecodingmachine/gotenberg/internal/pkg/standarderror" "golang.org/x/sync/errgroup" ) @@ -37,24 +38,26 @@ type ChromeOptions struct { } func (p *chrome) Print(destination string) error { + const op = "printer.chrome.Print" + // FIXME duration not working with float duration := time.Duration(p.opts.WaitTimeout+p.opts.WaitDelay) * time.Second ctx, cancel := context.WithTimeout(context.Background(), duration) defer cancel() devt, err := devtool.New("http://localhost:9222").Version(ctx) if err != nil { - return err + 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 err + 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 fmt.Errorf("creating new browser context: %v", err) + return &standarderror.Error{Op: op, Err: err} } // create a new blank target with the new browser context. createTargetArgs := target. @@ -62,13 +65,13 @@ func (p *chrome) Print(destination string) error { SetBrowserContextID(newContextTarget.BrowserContextID) newTarget, err := devtClient.Target.CreateTarget(ctx, createTargetArgs) if err != nil { - return fmt.Errorf("creating new blank target: %v", err) + 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 fmt.Errorf("connecting client to blank target: %v", err) + return &standarderror.Error{Op: op, Err: err} } defer newContextConn.Close() // nolint: errcheck // create a new CDP Client that uses newContextConn. @@ -83,10 +86,10 @@ func (p *chrome) Print(destination string) error { func() error { return targetClient.Page.Enable(ctx) }, func() error { return targetClient.Runtime.Enable(ctx) }, ); err != nil { - return err + return &standarderror.Error{Op: op, Err: err} } if err := p.navigate(ctx, targetClient); err != nil { - return err + return &standarderror.Error{Op: op, Err: err} } print, err := targetClient.Page.PrintToPDF( ctx, @@ -104,41 +107,42 @@ func (p *chrome) Print(destination string) error { SetPrintBackground(true), ) if err != nil { - return fmt.Errorf("printing page to PDF: %v", err) + return &standarderror.Error{Op: op, Err: err} } if err := ioutil.WriteFile(destination, print.Data, 0644); err != nil { - return fmt.Errorf("%s: writing file: %v", destination, err) + return &standarderror.Error{Op: op, Err: err} } return nil } func (p *chrome) navigate(ctx context.Context, client *cdp.Client) error { + const op = "printer.chrome.navigate" // make sure Page events are enabled. if err := client.Page.Enable(ctx); err != nil { - return err + return &standarderror.Error{Op: op, Err: err} } // make sure Network events are enabled. if err := client.Network.Enable(ctx, nil); err != nil { - return err + return &standarderror.Error{Op: op, Err: err} } // create all clients for events. domContentEventFired, err := client.Page.DOMContentEventFired(ctx) if err != nil { - return err + return &standarderror.Error{Op: op, Err: err} } defer domContentEventFired.Close() // nolint: errcheck loadEventFired, err := client.Page.LoadEventFired(ctx) if err != nil { - return err + return &standarderror.Error{Op: op, Err: err} } defer loadEventFired.Close() // nolint: errcheck loadingFinished, err := client.Network.LoadingFinished(ctx) if err != nil { - return err + return &standarderror.Error{Op: op, Err: err} } defer loadingFinished.Close() // nolint: errcheck if _, err := client.Page.Navigate(ctx, page.NewNavigateArgs(p.url)); err != nil { - return err + return &standarderror.Error{Op: op, Err: err} } if err := runBatch( // wait for all events. @@ -146,9 +150,10 @@ func (p *chrome) navigate(ctx context.Context, client *cdp.Client) error { func() error { _, err := loadEventFired.Recv(); return err }, func() error { _, err := loadingFinished.Recv(); return err }, ); err != nil { - return err + return &standarderror.Error{Op: op, Err: err} } // wait for a given amount of time (useful for javascript delay). + // FIXME duration not working with float time.Sleep(time.Duration(p.opts.WaitDelay) * time.Second) return nil } diff --git a/internal/pkg/printer/markdown.go b/internal/pkg/printer/markdown.go index 9c84830c..07ea3b55 100644 --- a/internal/pkg/printer/markdown.go +++ b/internal/pkg/printer/markdown.go @@ -10,27 +10,29 @@ import ( "github.com/labstack/gommon/random" "github.com/microcosm-cc/bluemonday" "github.com/russross/blackfriday/v2" + "github.com/thecodingmachine/gotenberg/internal/pkg/standarderror" ) // NewMarkdown returns a Markdown printer. func NewMarkdown(fpath string, opts *ChromeOptions) (Printer, error) { + const op = "printer.NewMarkdown" tmpl, err := template. New(filepath.Base(fpath)). Funcs(template.FuncMap{"toHTML": markdownToHTML}). ParseFiles(fpath) if err != nil { - return nil, fmt.Errorf("%s: parsing template: %v", fpath, err) + return nil, &standarderror.Error{Op: op, Err: err} } dirPath := filepath.Dir(fpath) data := &templateData{DirPath: dirPath} var buffer bytes.Buffer if err := tmpl.Execute(&buffer, data); err != nil { - return nil, fmt.Errorf("%s: executing template: %v", fpath, err) + return nil, &standarderror.Error{Op: op, Err: err} } baseFilename := random.String(32) dst := fmt.Sprintf("%s/%s.html", dirPath, baseFilename) if err := ioutil.WriteFile(dst, buffer.Bytes(), 0644); err != nil { - return nil, fmt.Errorf("%s: writing file: %v", dst, err) + return nil, &standarderror.Error{Op: op, Err: err} } URL := fmt.Sprintf("file://%s", dst) return &chrome{ @@ -44,10 +46,11 @@ type templateData struct { } func markdownToHTML(dirPath, filename string) (template.HTML, error) { + const op = "printer.markdownToHTML" fpath := fmt.Sprintf("%s/%s", dirPath, filename) b, err := ioutil.ReadFile(fpath) if err != nil { - return "", fmt.Errorf("%s: reading file: %v", fpath, err) + return "", &standarderror.Error{Op: op, Err: err} } unsafe := blackfriday.Run(b) content := bluemonday.UGCPolicy().SanitizeBytes(unsafe) diff --git a/internal/pkg/printer/merge.go b/internal/pkg/printer/merge.go index d21c2420..6393f5cf 100644 --- a/internal/pkg/printer/merge.go +++ b/internal/pkg/printer/merge.go @@ -2,9 +2,10 @@ package printer import ( "context" - "fmt" "os/exec" "time" + + "github.com/thecodingmachine/gotenberg/internal/pkg/standarderror" ) type merge struct { @@ -28,7 +29,9 @@ func NewMerge(fpaths []string, opts *MergeOptions) Printer { } func (p *merge) Print(destination string) error { + const op = "printer.merge.Print" if p.ctx == nil { + // FIXME duration not working with float ctx, cancel := context.WithTimeout(context.Background(), time.Duration(p.opts.WaitTimeout)*time.Second) defer cancel() p.ctx = ctx @@ -39,7 +42,7 @@ func (p *merge) Print(destination string) error { cmd := exec.CommandContext(p.ctx, "pdftk", cmdArgs...) _, err := cmd.Output() if err != nil { - return fmt.Errorf("pdtk: %v", err) + return &standarderror.Error{Op: op, Err: err} } return nil } diff --git a/internal/pkg/printer/office.go b/internal/pkg/printer/office.go index 8e83824e..214da42b 100644 --- a/internal/pkg/printer/office.go +++ b/internal/pkg/printer/office.go @@ -10,6 +10,7 @@ import ( "time" "github.com/labstack/gommon/random" + "github.com/thecodingmachine/gotenberg/internal/pkg/standarderror" ) type office struct { @@ -33,6 +34,8 @@ func NewOffice(fpaths []string, opts *OfficeOptions) Printer { } func (p *office) Print(destination string) error { + const op = "printer.office.Print" + // FIXME duration not working with float ctx, cancel := context.WithTimeout(context.Background(), time.Duration(p.opts.WaitTimeout)*time.Second) defer cancel() fpaths := make([]string, len(p.fpaths)) @@ -41,24 +44,31 @@ func (p *office) Print(destination string) error { 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 err + return &standarderror.Error{Op: op, Err: err} } fpaths[i] = tmpDest } if len(fpaths) == 1 { - return os.Rename(fpaths[0], destination) + if err := os.Rename(fpaths[0], destination); err != nil { + return &standarderror.Error{Op: op, Err: err} + } + return nil } m := &merge{ ctx: ctx, fpaths: fpaths, } - return m.Print(destination) + if err := m.Print(destination); err != nil { + return &standarderror.Error{Op: op, Err: err} + } + return nil } // nolint: gochecknoglobals var mu sync.Mutex func unoconv(ctx context.Context, fpath, destination string, opts *OfficeOptions) error { + const op = "printer.unoconv" mu.Lock() defer mu.Unlock() cmdArgs := []string{ @@ -76,7 +86,7 @@ func unoconv(ctx context.Context, fpath, destination string, opts *OfficeOptions ) _, err := cmd.Output() if err != nil { - return fmt.Errorf("unoconv: %v", err) + return &standarderror.Error{Op: op, Err: err} } return nil }