Use xdefer to append issue creation message
This commit is contained in:
parent
1658a2e0f4
commit
49f0a049fe
3 changed files with 18 additions and 8 deletions
|
|
@ -22,6 +22,7 @@ import (
|
||||||
"oss.terrastruct.com/d2/lib/png"
|
"oss.terrastruct.com/d2/lib/png"
|
||||||
"oss.terrastruct.com/d2/lib/version"
|
"oss.terrastruct.com/d2/lib/version"
|
||||||
"oss.terrastruct.com/d2/lib/xmain"
|
"oss.terrastruct.com/d2/lib/xmain"
|
||||||
|
"oss.terrastruct.com/xdefer"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
@ -122,7 +123,7 @@ func run(ctx context.Context, ms *xmain.State) (err error) {
|
||||||
defer func() {
|
defer func() {
|
||||||
cleanupErr := pw.Cleanup()
|
cleanupErr := pw.Cleanup()
|
||||||
if cleanupErr != nil {
|
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
|
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)
|
input, err := ms.ReadPath(inputPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -176,6 +177,7 @@ func compile(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, input
|
||||||
return nil, err
|
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)
|
svg, err := d2svg.Render(d)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
||||||
|
|
@ -332,7 +332,6 @@ func (w *watcher) compileLoop(ctx context.Context) error {
|
||||||
if filepath.Ext(w.outputPath) == ".png" && !w.pw.Browser.IsConnected() {
|
if filepath.Ext(w.outputPath) == ".png" && !w.pw.Browser.IsConnected() {
|
||||||
newPW, err := w.pw.RestartBrowser()
|
newPW, err := w.pw.RestartBrowser()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = fmt.Errorf("png exporter has disconnected")
|
|
||||||
w.ms.Log.Error.Print(err)
|
w.ms.Log.Error.Print(err)
|
||||||
w.broadcast(&compileResult{
|
w.broadcast(&compileResult{
|
||||||
Err: err.Error(),
|
Err: err.Error(),
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ import (
|
||||||
|
|
||||||
"github.com/playwright-community/playwright-go"
|
"github.com/playwright-community/playwright-go"
|
||||||
"oss.terrastruct.com/d2/lib/xmain"
|
"oss.terrastruct.com/d2/lib/xmain"
|
||||||
|
"oss.terrastruct.com/xdefer"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Playwright struct {
|
type Playwright struct {
|
||||||
|
|
@ -21,6 +22,8 @@ type Playwright struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pw *Playwright) RestartBrowser() (newPW Playwright, err error) {
|
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 {
|
if err = pw.Browser.Close(); err != nil {
|
||||||
return Playwright{}, err
|
return Playwright{}, err
|
||||||
}
|
}
|
||||||
|
|
@ -28,6 +31,8 @@ func (pw *Playwright) RestartBrowser() (newPW Playwright, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pw *Playwright) Cleanup() (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 {
|
if err = pw.Browser.Close(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -57,7 +62,9 @@ func startPlaywright(pw *playwright.Playwright) (Playwright, error) {
|
||||||
}, nil
|
}, 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
|
// check if playwright driver/browsers are installed and up to date
|
||||||
// https://github.com/playwright-community/playwright-go/blob/8e8f670b5fa7ba5365ae4bfc123fea4aac359763/run.go#L64.
|
// https://github.com/playwright-community/playwright-go/blob/8e8f670b5fa7ba5365ae4bfc123fea4aac359763/run.go#L64.
|
||||||
driver, err := playwright.NewDriver(&playwright.RunOptions{})
|
driver, err := playwright.NewDriver(&playwright.RunOptions{})
|
||||||
|
|
@ -73,7 +80,7 @@ func InitPlaywright() (Playwright, error) {
|
||||||
cmd := exec.Command(driver.DriverBinaryLocation, "--version")
|
cmd := exec.Command(driver.DriverBinaryLocation, "--version")
|
||||||
output, err := cmd.Output()
|
output, err := cmd.Output()
|
||||||
if err != nil {
|
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)) {
|
if !bytes.Contains(output, []byte(driver.Version)) {
|
||||||
err = playwright.Install()
|
err = playwright.Install()
|
||||||
|
|
@ -82,7 +89,7 @@ func InitPlaywright() (Playwright, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} 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()
|
pw, err := playwright.Run()
|
||||||
|
|
@ -96,8 +103,10 @@ func InitPlaywright() (Playwright, error) {
|
||||||
var genPNGScript string
|
var genPNGScript string
|
||||||
|
|
||||||
func ExportPNG(ms *xmain.State, page playwright.Page, svg []byte) (outputImage []byte, err error) {
|
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 {
|
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)
|
encodedSVG := base64.StdEncoding.EncodeToString(svg)
|
||||||
|
|
@ -112,7 +121,7 @@ func ExportPNG(ms *xmain.State, page playwright.Page, svg []byte) (outputImage [
|
||||||
if len(pngString) > 50 {
|
if len(pngString) > 50 {
|
||||||
pngString = pngString[0: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):]
|
splicedPNGString := pngString[len(pngPrefix):]
|
||||||
return base64.StdEncoding.DecodeString(splicedPNGString)
|
return base64.StdEncoding.DecodeString(splicedPNGString)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue