diff --git a/cmd/d2/main.go b/cmd/d2/main.go index 443c65acf..e423166bc 100644 --- a/cmd/d2/main.go +++ b/cmd/d2/main.go @@ -22,7 +22,6 @@ 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() { @@ -123,7 +122,7 @@ func run(ctx context.Context, ms *xmain.State) (err error) { defer func() { cleanupErr := pw.Cleanup() if cleanupErr != nil { - ms.Log.Error.Printf(cleanupErr.Error()) + ms.Log.Error.Printf("error cleaning up png exporter: %v", cleanupErr.Error()) } }() } @@ -156,7 +155,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, err error) { +func compile(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, inputPath, outputPath string, page playwright.Page) ([]byte, error) { input, err := ms.ReadPath(inputPath) if err != nil { return nil, err @@ -177,7 +176,6 @@ 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 5194ce3fa..89b74da27 100644 --- a/cmd/d2/watch.go +++ b/cmd/d2/watch.go @@ -332,6 +332,7 @@ 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 509faf6b9..e1cc821f1 100644 --- a/lib/png/png.go +++ b/lib/png/png.go @@ -12,7 +12,6 @@ import ( "github.com/playwright-community/playwright-go" "oss.terrastruct.com/d2/lib/xmain" - "oss.terrastruct.com/xdefer" ) type Playwright struct { @@ -22,8 +21,6 @@ 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 } @@ -31,8 +28,6 @@ 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 } @@ -62,9 +57,7 @@ func startPlaywright(pw *playwright.Playwright) (Playwright, error) { }, nil } -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") - +func InitPlaywright() (Playwright, error) { // 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{}) @@ -80,7 +73,7 @@ func InitPlaywright() (_ Playwright, err error) { cmd := exec.Command(driver.DriverBinaryLocation, "--version") output, err := cmd.Output() if err != nil { - return Playwright{}, err + return Playwright{}, fmt.Errorf("error installing png exporter: %v\nplease report this issue here: https://github.com/terrastruct/d2/issues/new", err.Error()) } if !bytes.Contains(output, []byte(driver.Version)) { err = playwright.Install() @@ -89,7 +82,7 @@ func InitPlaywright() (_ Playwright, err error) { } } } else { - return Playwright{}, err + return Playwright{}, fmt.Errorf("could not install png exporter: %v\nplease report this issue here: https://github.com/terrastruct/d2/issues/new", err.Error()) } pw, err := playwright.Run() @@ -103,10 +96,8 @@ func InitPlaywright() (_ Playwright, err 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") + return nil, fmt.Errorf("png exporter was not initialized properly\nplease report this issue here: https://github.com/terrastruct/d2/issues/new") } encodedSVG := base64.StdEncoding.EncodeToString(svg) @@ -121,7 +112,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", pngString) + return nil, fmt.Errorf("invalid PNG: %v\nplease report this issue here: https://github.com/terrastruct/d2/issues/new", pngString) } splicedPNGString := pngString[len(pngPrefix):] return base64.StdEncoding.DecodeString(splicedPNGString)