From 49f0a049fef58eb909c3017cd54ec399120958b0 Mon Sep 17 00:00:00 2001 From: Bernard Xie Date: Thu, 17 Nov 2022 17:22:08 -0800 Subject: [PATCH] Use xdefer to append issue creation message --- cmd/d2/main.go | 6 ++++-- cmd/d2/watch.go | 1 - lib/png/png.go | 19 ++++++++++++++----- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/cmd/d2/main.go b/cmd/d2/main.go index e423166bc..443c65acf 100644 --- a/cmd/d2/main.go +++ b/cmd/d2/main.go @@ -22,6 +22,7 @@ import ( "oss.terrastruct.com/d2/lib/png" "oss.terrastruct.com/d2/lib/version" "oss.terrastruct.com/d2/lib/xmain" + "oss.terrastruct.com/xdefer" ) func main() { @@ -122,7 +123,7 @@ func run(ctx context.Context, ms *xmain.State) (err error) { defer func() { cleanupErr := pw.Cleanup() if cleanupErr != nil { - ms.Log.Error.Printf("error cleaning up png exporter: %v", cleanupErr.Error()) + ms.Log.Error.Printf(cleanupErr.Error()) } }() } @@ -155,7 +156,7 @@ func run(ctx context.Context, ms *xmain.State) (err error) { return nil } -func compile(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, inputPath, outputPath string, page playwright.Page) ([]byte, error) { +func compile(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, inputPath, outputPath string, page playwright.Page) (_ []byte, err error) { input, err := ms.ReadPath(inputPath) if err != nil { return nil, err @@ -176,6 +177,7 @@ func compile(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, input return nil, err } + defer xdefer.Errorf(&err, "failed to compile d2 \nplease report this issue here: https://github.com/terrastruct/d2/issues/new") svg, err := d2svg.Render(d) if err != nil { return nil, err diff --git a/cmd/d2/watch.go b/cmd/d2/watch.go index 89b74da27..5194ce3fa 100644 --- a/cmd/d2/watch.go +++ b/cmd/d2/watch.go @@ -332,7 +332,6 @@ func (w *watcher) compileLoop(ctx context.Context) error { if filepath.Ext(w.outputPath) == ".png" && !w.pw.Browser.IsConnected() { newPW, err := w.pw.RestartBrowser() if err != nil { - err = fmt.Errorf("png exporter has disconnected") w.ms.Log.Error.Print(err) w.broadcast(&compileResult{ Err: err.Error(), diff --git a/lib/png/png.go b/lib/png/png.go index e1cc821f1..509faf6b9 100644 --- a/lib/png/png.go +++ b/lib/png/png.go @@ -12,6 +12,7 @@ import ( "github.com/playwright-community/playwright-go" "oss.terrastruct.com/d2/lib/xmain" + "oss.terrastruct.com/xdefer" ) type Playwright struct { @@ -21,6 +22,8 @@ type Playwright struct { } func (pw *Playwright) RestartBrowser() (newPW Playwright, err error) { + defer xdefer.Errorf(&err, "png exporter has disconnected\nplease report this issue here: https://github.com/terrastruct/d2/issues/new") + if err = pw.Browser.Close(); err != nil { return Playwright{}, err } @@ -28,6 +31,8 @@ func (pw *Playwright) RestartBrowser() (newPW Playwright, err error) { } func (pw *Playwright) Cleanup() (err error) { + defer xdefer.Errorf(&err, "failed to clean up png exporter\nplease report this issue here: https://github.com/terrastruct/d2/issues/new") + if err = pw.Browser.Close(); err != nil { return err } @@ -57,7 +62,9 @@ func startPlaywright(pw *playwright.Playwright) (Playwright, error) { }, nil } -func InitPlaywright() (Playwright, error) { +func InitPlaywright() (_ Playwright, err error) { + defer xdefer.Errorf(&err, "failed to initialize png exporter\nplease report this issue here: https://github.com/terrastruct/d2/issues/new") + // check if playwright driver/browsers are installed and up to date // https://github.com/playwright-community/playwright-go/blob/8e8f670b5fa7ba5365ae4bfc123fea4aac359763/run.go#L64. driver, err := playwright.NewDriver(&playwright.RunOptions{}) @@ -73,7 +80,7 @@ func InitPlaywright() (Playwright, error) { cmd := exec.Command(driver.DriverBinaryLocation, "--version") output, err := cmd.Output() if err != nil { - return Playwright{}, fmt.Errorf("error installing png exporter: %v\nplease report this issue here: https://github.com/terrastruct/d2/issues/new", err.Error()) + return Playwright{}, err } if !bytes.Contains(output, []byte(driver.Version)) { err = playwright.Install() @@ -82,7 +89,7 @@ func InitPlaywright() (Playwright, error) { } } } else { - return Playwright{}, fmt.Errorf("could not install png exporter: %v\nplease report this issue here: https://github.com/terrastruct/d2/issues/new", err.Error()) + return Playwright{}, err } pw, err := playwright.Run() @@ -96,8 +103,10 @@ func InitPlaywright() (Playwright, error) { var genPNGScript string func ExportPNG(ms *xmain.State, page playwright.Page, svg []byte) (outputImage []byte, err error) { + defer xdefer.Errorf(&err, "failed to export png") + if page == nil { - return nil, fmt.Errorf("png exporter was not initialized properly\nplease report this issue here: https://github.com/terrastruct/d2/issues/new") + return nil, fmt.Errorf("png exporter was not initialized properly") } encodedSVG := base64.StdEncoding.EncodeToString(svg) @@ -112,7 +121,7 @@ func ExportPNG(ms *xmain.State, page playwright.Page, svg []byte) (outputImage [ if len(pngString) > 50 { pngString = pngString[0:50] + "..." } - return nil, fmt.Errorf("invalid PNG: %v\nplease report this issue here: https://github.com/terrastruct/d2/issues/new", pngString) + return nil, fmt.Errorf("invalid PNG: %v", pngString) } splicedPNGString := pngString[len(pngPrefix):] return base64.StdEncoding.DecodeString(splicedPNGString)