Skip to content

Commit

Permalink
Clean up internal robot error passing
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Nov 18, 2014
1 parent c5906a9 commit 665b3b5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
10 changes: 3 additions & 7 deletions connection.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package gobot

import (
"errors"
"fmt"
"log"
)

Expand All @@ -29,8 +27,7 @@ func (c *connections) Each(f func(Connection)) {
}

// Start initializes all the connections.
func (c *connections) Start() error {
var err error
func (c *connections) Start() (err error) {
log.Println("Starting connections...")
for _, connection := range *c {
info := "Starting connection " + connection.Name()
Expand All @@ -40,11 +37,10 @@ func (c *connections) Start() error {
log.Println(info + "...")
err = connection.Connect()
if err != nil {
err = errors.New(fmt.Sprintf("Could not start connection: %v", err))
break
return
}
}
return err
return
}

// Finalize finishes all the connections.
Expand Down
11 changes: 7 additions & 4 deletions gobot.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,17 @@ func (g *Gobot) Command(name string) func(map[string]interface{}) interface{} {
}

// Start runs the main Gobot event loop
func (g *Gobot) Start() error {
err := g.robots.Start()
func (g *Gobot) Start() (err error) {
err = g.robots.Start()
if err != nil {
return err
log.Println(err)
}

c := make(chan os.Signal, 1)
g.trap(c)
if err != nil {
c <- os.Interrupt
}

// waiting for interrupt coming on the channel
_ = <-c
Expand All @@ -72,7 +75,7 @@ func (g *Gobot) Start() error {
r.Devices().Halt()
r.Connections().Finalize()
})
return nil
return err
}

// Robots fetch all robots associated with this Gobot instance.
Expand Down
21 changes: 10 additions & 11 deletions robot.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gobot

import (
"errors"
"fmt"
"log"
)
Expand Down Expand Up @@ -34,14 +33,14 @@ func (r *robots) Len() int {

// Start initialises the event loop. All robots that were added will
// be automtically started as a result of this call.
func (r *robots) Start() error {
func (r *robots) Start() (err error) {
for _, robot := range *r {
err := robot.Start()
err = robot.Start()
if err != nil {
return err
return
}
}
return nil
return
}

// Each enumerates thru the robots and calls specified function
Expand Down Expand Up @@ -85,7 +84,7 @@ func NewRobot(name string, v ...interface{}) *Robot {
case func():
r.Work = v[i].(func())
default:
fmt.Println("Unknown argument passed to NewRobot")
log.Println("Unknown argument passed to NewRobot")
}
}

Expand All @@ -110,13 +109,13 @@ func (r *Robot) Command(name string) func(map[string]interface{}) interface{} {
// Start a robot instance and runs it's work function if any. You should not
// need to manually start a robot if already part of a Gobot application as the
// robot will be automatically started for you.
func (r *Robot) Start() error {
func (r *Robot) Start() (err error) {
log.Println("Starting Robot", r.Name, "...")
if err := r.Connections().Start(); err != nil {
return errors.New(fmt.Sprintf("Could not start connections: %v", err))
if err = r.Connections().Start(); err != nil {
return
}
if err := r.Devices().Start(); err != nil {
return errors.New(fmt.Sprintf("Could not start devices %v", err))
if err = r.Devices().Start(); err != nil {
return err
}
if r.Work != nil {
log.Println("Starting work...")
Expand Down

0 comments on commit 665b3b5

Please sign in to comment.