all: simplify some error variables

There's no need to put the error variables in a larger scope, nor define
them earlier than necessary. If anything, it makes the code harder to
follow, such as figuring out when nil errors are returned.
This commit is contained in:
Daniel Martí 2017-11-25 20:20:52 +00:00 committed by Kenneth Shaw
parent 7ed029cf24
commit 335d22d376
7 changed files with 14 additions and 38 deletions

View File

@ -28,14 +28,11 @@ type Tasks []Action
// Do executes the list of Actions sequentially, using the provided context and // Do executes the list of Actions sequentially, using the provided context and
// frame handler. // frame handler.
func (t Tasks) Do(ctxt context.Context, h cdp.Handler) error { func (t Tasks) Do(ctxt context.Context, h cdp.Handler) error {
var err error
// TODO: put individual task timeouts from context here // TODO: put individual task timeouts from context here
for _, a := range t { for _, a := range t {
// ctxt, cancel = context.WithTimeout(ctxt, timeout) // ctxt, cancel = context.WithTimeout(ctxt, timeout)
// defer cancel() // defer cancel()
err = a.Do(ctxt, h) if err := a.Do(ctxt, h); err != nil {
if err != nil {
return err return err
} }
} }

View File

@ -42,8 +42,6 @@ type CDP struct {
// New creates and starts a new CDP instance. // New creates and starts a new CDP instance.
func New(ctxt context.Context, opts ...Option) (*CDP, error) { func New(ctxt context.Context, opts ...Option) (*CDP, error) {
var err error
c := &CDP{ c := &CDP{
handlers: make([]*TargetHandler, 0), handlers: make([]*TargetHandler, 0),
handlerMap: make(map[string]int), handlerMap: make(map[string]int),
@ -54,14 +52,14 @@ func New(ctxt context.Context, opts ...Option) (*CDP, error) {
// apply options // apply options
for _, o := range opts { for _, o := range opts {
err = o(c) if err := o(c); err != nil {
if err != nil {
return nil, err return nil, err
} }
} }
// check for supplied runner, if none then create one // check for supplied runner, if none then create one
if c.r == nil && c.watch == nil { if c.r == nil && c.watch == nil {
var err error
c.r, err = runner.Run(ctxt, c.opts...) c.r, err = runner.Run(ctxt, c.opts...)
if err != nil { if err != nil {
return nil, err return nil, err
@ -121,8 +119,7 @@ func (c *CDP) AddTarget(ctxt context.Context, t client.Target) {
} }
// run // run
err = h.Run(ctxt) if err := h.Run(ctxt); err != nil {
if err != nil {
c.errorf("could not start handler for %s: %v", t, err) c.errorf("could not start handler for %s: %v", t, err)
return return
} }

View File

@ -28,8 +28,6 @@ func Evaluate(expression string, res interface{}, opts ...EvaluateOption) Action
} }
return ActionFunc(func(ctxt context.Context, h cdp.Handler) error { return ActionFunc(func(ctxt context.Context, h cdp.Handler) error {
var err error
// set up parameters // set up parameters
p := rundom.Evaluate(expression) p := rundom.Evaluate(expression)
switch res.(type) { switch res.(type) {

View File

@ -80,8 +80,6 @@ func NewTargetHandler(t client.Target, logf, debugf, errorf LogFunc) (*TargetHan
// //
// Callers can stop Run by closing the passed context. // Callers can stop Run by closing the passed context.
func (h *TargetHandler) Run(ctxt context.Context) error { func (h *TargetHandler) Run(ctxt context.Context) error {
var err error
// reset // reset
h.Lock() h.Lock()
h.frames = make(map[cdp.FrameID]*cdp.Frame) h.frames = make(map[cdp.FrameID]*cdp.Frame)
@ -107,8 +105,7 @@ func (h *TargetHandler) Run(ctxt context.Context) error {
dom.Enable(), dom.Enable(),
css.Enable(), css.Enable(),
} { } {
err = a.Do(ctxt, h) if err := a.Do(ctxt, h); err != nil {
if err != nil {
return fmt.Errorf("unable to execute %s: %v", reflect.TypeOf(a), err) return fmt.Errorf("unable to execute %s: %v", reflect.TypeOf(a), err)
} }
} }
@ -175,25 +172,23 @@ func (h *TargetHandler) run(ctxt context.Context) {
} }
}() }()
var err error
// process queues // process queues
for { for {
select { select {
case ev := <-h.qevents: case ev := <-h.qevents:
err = h.processEvent(ctxt, ev) err := h.processEvent(ctxt, ev)
if err != nil { if err != nil {
h.errorf("could not process event %s: %v", ev.Method, err) h.errorf("could not process event %s: %v", ev.Method, err)
} }
case res := <-h.qres: case res := <-h.qres:
err = h.processResult(res) err := h.processResult(res)
if err != nil { if err != nil {
h.errorf("could not process result for message %d: %v", res.ID, err) h.errorf("could not process result for message %d: %v", res.ID, err)
} }
case cmd := <-h.qcmd: case cmd := <-h.qcmd:
err = h.processCommand(cmd) err := h.processCommand(cmd)
if err != nil { if err != nil {
h.errorf("could not process command message %d: %v", cmd.ID, err) h.errorf("could not process command message %d: %v", cmd.ID, err)
} }

View File

@ -28,8 +28,6 @@ type Pool struct {
// NewPool creates a new Chrome runner pool. // NewPool creates a new Chrome runner pool.
func NewPool(opts ...PoolOption) (*Pool, error) { func NewPool(opts ...PoolOption) (*Pool, error) {
var err error
p := &Pool{ p := &Pool{
start: DefaultPoolStartPort, start: DefaultPoolStartPort,
end: DefaultPoolEndPort, end: DefaultPoolEndPort,
@ -41,13 +39,12 @@ func NewPool(opts ...PoolOption) (*Pool, error) {
// apply opts // apply opts
for _, o := range opts { for _, o := range opts {
err = o(p) if err := o(p); err != nil {
if err != nil {
return nil, err return nil, err
} }
} }
return p, err return p, nil
} }
// Shutdown releases all the pool resources. // Shutdown releases all the pool resources.

View File

@ -445,8 +445,6 @@ func Screenshot(sel interface{}, picbuf *[]byte, opts ...QueryOption) Action {
return fmt.Errorf("selector `%s` did not return any nodes", sel) return fmt.Errorf("selector `%s` did not return any nodes", sel)
} }
var err error
// get box model // get box model
box, err := dom.GetBoxModel().WithNodeID(nodes[0].NodeID).Do(ctxt, h) box, err := dom.GetBoxModel().WithNodeID(nodes[0].NodeID).Do(ctxt, h)
if err != nil { if err != nil {
@ -596,9 +594,8 @@ func ScrollIntoView(sel interface{}, opts ...QueryOption) Action {
return fmt.Errorf("selector `%s` did not return any nodes", sel) return fmt.Errorf("selector `%s` did not return any nodes", sel)
} }
var err error
var pos []int var pos []int
err = EvaluateAsDevTools(fmt.Sprintf(scrollIntoViewJS, nodes[0].FullXPath()), &pos).Do(ctxt, h) err := EvaluateAsDevTools(fmt.Sprintf(scrollIntoViewJS, nodes[0].FullXPath()), &pos).Do(ctxt, h)
if err != nil { if err != nil {
return err return err
} }

11
sel.go
View File

@ -221,9 +221,8 @@ func ByNodeID(s *Selector) {
} }
ByFunc(func(ctxt context.Context, h cdp.Handler, n *cdp.Node) ([]cdp.NodeID, error) { ByFunc(func(ctxt context.Context, h cdp.Handler, n *cdp.Node) ([]cdp.NodeID, error) {
var err error
for _, id := range ids { for _, id := range ids {
err = dom.RequestChildNodes(id).WithPierce(true).Do(ctxt, h) err := dom.RequestChildNodes(id).WithPierce(true).Do(ctxt, h)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -296,10 +295,8 @@ func NodeReady(s *Selector) {
// NodeVisible is a query option to wait until the element is visible. // NodeVisible is a query option to wait until the element is visible.
func NodeVisible(s *Selector) { func NodeVisible(s *Selector) {
WaitFunc(s.waitReady(func(ctxt context.Context, h cdp.Handler, n *cdp.Node) error { WaitFunc(s.waitReady(func(ctxt context.Context, h cdp.Handler, n *cdp.Node) error {
var err error
// check box model // check box model
_, err = dom.GetBoxModel().WithNodeID(n.NodeID).Do(ctxt, h) _, err := dom.GetBoxModel().WithNodeID(n.NodeID).Do(ctxt, h)
if err != nil { if err != nil {
if isCouldNotComputeBoxModelError(err) { if isCouldNotComputeBoxModelError(err) {
return ErrNotVisible return ErrNotVisible
@ -324,10 +321,8 @@ func NodeVisible(s *Selector) {
// NodeNotVisible is a query option to wait until the element is not visible. // NodeNotVisible is a query option to wait until the element is not visible.
func NodeNotVisible(s *Selector) { func NodeNotVisible(s *Selector) {
WaitFunc(s.waitReady(func(ctxt context.Context, h cdp.Handler, n *cdp.Node) error { WaitFunc(s.waitReady(func(ctxt context.Context, h cdp.Handler, n *cdp.Node) error {
var err error
// check box model // check box model
_, err = dom.GetBoxModel().WithNodeID(n.NodeID).Do(ctxt, h) _, err := dom.GetBoxModel().WithNodeID(n.NodeID).Do(ctxt, h)
if err != nil { if err != nil {
if isCouldNotComputeBoxModelError(err) { if isCouldNotComputeBoxModelError(err) {
return nil return nil