playwright-go icon indicating copy to clipboard operation
playwright-go copied to clipboard

[Feature]: Please revisit the PR #219, issue: Investigate into Must* methods #103

Open caner-cetin opened this issue 6 months ago • 0 comments

Is your feature request related to a problem? Please describe.

package main

import (
	"github.com/playwright-community/playwright-go"
	"log"
)

func main() {
	if err := playwright.Install(&playwright.RunOptions{Browsers: []string{"chromium"}}); err != nil {
		log.Fatalf("failed to install chromium: %v", err)
	}
	pw, err := playwright.Run()
	if err != nil {
		log.Fatalf("failed to run chromium: %v", err)
	}
	browser, err := pw.Chromium.Launch()
	if err != nil {
		log.Fatalf("failed to run browser: %v", err)
	}
	context, err := browser.NewContext()
	if err != nil {
		log.Fatalf("failed to launch new context: %v", err)
	}
	page, err := context.NewPage()
	if err != nil {
		log.Fatalf("failed to launch new page: %v", err)
	}
	resp, err := page.Goto("https://canwestoptypingerr!=nilateveryfunctioncall.to")
	if err != nil {
		log.Fatalf("failed to navigate to page: %v", err)
	}
	resp.URL() // ...
}

Currently, there are no Must methods that were supposed to come with https://github.com/playwright-community/playwright-go/pull/219 but were rejected for this reason

Let's not ship it for now please, since there was not a single upvote. But still thank you for the contribution, we can pick it up once there is a higher demand in that feature.

3 years later, everything, absolutely every single function call, except the simple getters/setters like Response.URL throws errors, and it gets so, so frustrating after a long while, thanks to repeating the same err != nil over and over and over again. Like, we could just do this:

	page, err := playwright.MustRun(&playwright.RunOptions{}).
		MustLaunch(&playwright.BrowserTypeLaunchOptions{}).
		MustNewContext(&playwright.BrowserNewContextOptions{}).
		MustNewPage() // done!

Even the standard library has Must methods, see regexp.MustCompile. fastjson package has Must methods, and parsing JSONs in one-liners is so elegant that I use it in every single project where I need it.

Please, please, revisit #219.

Describe the solution you'd like Let us chain calls, panic everything on the way, and let us recover if we want to.

caner-cetin avatar May 22 '25 09:05 caner-cetin