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
// frame handler.
func (t Tasks) Do(ctxt context.Context, h cdp.Handler) error {
var err error
// TODO: put individual task timeouts from context here
for _, a := range t {
// ctxt, cancel = context.WithTimeout(ctxt, timeout)
// defer cancel()
err = a.Do(ctxt, h)
if err != nil {
if err := a.Do(ctxt, h); err != nil {
return err
}
}

View File

@ -42,8 +42,6 @@ type CDP struct {
// New creates and starts a new CDP instance.
func New(ctxt context.Context, opts ...Option) (*CDP, error) {
var err error
c := &CDP{
handlers: make([]*TargetHandler, 0),
handlerMap: make(map[string]int),
@ -54,14 +52,14 @@ func New(ctxt context.Context, opts ...Option) (*CDP, error) {
// apply options
for _, o := range opts {
err = o(c)
if err != nil {
if err := o(c); err != nil {
return nil, err
}
}
// check for supplied runner, if none then create one
if c.r == nil && c.watch == nil {
var err error
c.r, err = runner.Run(ctxt, c.opts...)
if err != nil {
return nil, err
@ -121,8 +119,7 @@ func (c *CDP) AddTarget(ctxt context.Context, t client.Target) {
}
// run
err = h.Run(ctxt)
if err != nil {
if err := h.Run(ctxt); err != nil {
c.errorf("could not start handler for %s: %v", t, err)
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 {
var err error
// set up parameters
p := rundom.Evaluate(expression)
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.
func (h *TargetHandler) Run(ctxt context.Context) error {
var err error
// reset
h.Lock()
h.frames = make(map[cdp.FrameID]*cdp.Frame)
@ -107,8 +105,7 @@ func (h *TargetHandler) Run(ctxt context.Context) error {
dom.Enable(),
css.Enable(),
} {
err = a.Do(ctxt, h)
if err != nil {
if err := a.Do(ctxt, h); err != nil {
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
for {
select {
case ev := <-h.qevents:
err = h.processEvent(ctxt, ev)
err := h.processEvent(ctxt, ev)
if err != nil {
h.errorf("could not process event %s: %v", ev.Method, err)
}
case res := <-h.qres:
err = h.processResult(res)
err := h.processResult(res)
if err != nil {
h.errorf("could not process result for message %d: %v", res.ID, err)
}
case cmd := <-h.qcmd:
err = h.processCommand(cmd)
err := h.processCommand(cmd)
if err != nil {
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.
func NewPool(opts ...PoolOption) (*Pool, error) {
var err error
p := &Pool{
start: DefaultPoolStartPort,
end: DefaultPoolEndPort,
@ -41,13 +39,12 @@ func NewPool(opts ...PoolOption) (*Pool, error) {
// apply opts
for _, o := range opts {
err = o(p)
if err != nil {
if err := o(p); err != nil {
return nil, err
}
}
return p, err
return p, nil
}
// 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)
}
var err error
// get box model
box, err := dom.GetBoxModel().WithNodeID(nodes[0].NodeID).Do(ctxt, h)
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)
}
var err error
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 {
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) {
var err error
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 {
return nil, err
}
@ -296,10 +295,8 @@ func NodeReady(s *Selector) {
// NodeVisible is a query option to wait until the element is visible.
func NodeVisible(s *Selector) {
WaitFunc(s.waitReady(func(ctxt context.Context, h cdp.Handler, n *cdp.Node) error {
var err error
// 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 isCouldNotComputeBoxModelError(err) {
return ErrNotVisible
@ -324,10 +321,8 @@ func NodeVisible(s *Selector) {
// NodeNotVisible is a query option to wait until the element is not visible.
func NodeNotVisible(s *Selector) {
WaitFunc(s.waitReady(func(ctxt context.Context, h cdp.Handler, n *cdp.Node) error {
var err error
// 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 isCouldNotComputeBoxModelError(err) {
return nil