Skip to content

Commit

Permalink
Pass error instead of panic
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Nov 12, 2014
1 parent c564e99 commit 17cbf2c
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 21 deletions.
4 changes: 2 additions & 2 deletions adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ type Adaptor struct {

// AdaptorInterface defines behaviour expected for a Gobot Adaptor
type AdaptorInterface interface {
Finalize() bool
Connect() bool
Finalize() error
Connect() error
Port() string
Name() string
Type() string
Expand Down
6 changes: 4 additions & 2 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gobot

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

Expand Down Expand Up @@ -37,8 +38,9 @@ func (c *connections) Start() error {
info = info + " on port " + connection.Port()
}
log.Println(info + "...")
if connection.Connect() == false {
err = errors.New("Could not start connection")
err = connection.Connect()
if err != nil {
err = errors.New(fmt.Sprintf("Could not start connection: %v", err))
break
}
}
Expand Down
6 changes: 4 additions & 2 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gobot

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

Expand Down Expand Up @@ -39,8 +40,9 @@ func (d *devices) Start() error {
info = info + " on pin " + device.Pin()
}
log.Println(info + "...")
if device.Start() == false {
err = errors.New("Could not start device")
err = device.Start()
if err != nil {
err = errors.New(fmt.Sprintf("Could not start device: %v", err))
break
}
}
Expand Down
4 changes: 2 additions & 2 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

// DriverInterface defines Driver expected behaviour
type DriverInterface interface {
Start() bool
Halt() bool
Start() error
Halt() error
Adaptor() AdaptorInterface
SetInterval(time.Duration)
Interval() time.Duration
Expand Down
8 changes: 6 additions & 2 deletions gobot.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ func (g *Gobot) Command(name string) func(map[string]interface{}) interface{} {
}

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

c := make(chan os.Signal, 1)
g.trap(c)
Expand All @@ -69,6 +72,7 @@ func (g *Gobot) Start() {
r.Devices().Halt()
r.Connections().Finalize()
})
return nil
}

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

import (
"errors"
"fmt"
"log"
)
Expand Down Expand Up @@ -33,13 +34,17 @@ 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() {
func (r *robots) Start() error {
for _, robot := range *r {
robot.Start()
err := robot.Start()
if err != nil {
return err
}
}
return nil
}

// Each enumerates thru the robts and calls specified function
// Each enumerates thru the robots and calls specified function
func (r *robots) Each(f func(*Robot)) {
for _, robot := range *r {
f(robot)
Expand Down Expand Up @@ -105,18 +110,19 @@ 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() {
func (r *Robot) Start() error {
log.Println("Starting Robot", r.Name, "...")
if err := r.Connections().Start(); err != nil {
panic("Could not start connections")
return errors.New(fmt.Sprintf("Could not start connections: %v", err))
}
if err := r.Devices().Start(); err != nil {
panic("Could not start devices")
return errors.New(fmt.Sprintf("Could not start devices %v", err))
}
if r.Work != nil {
log.Println("Starting work...")
r.Work()
}
return nil
}

// Devices retrieves all devices associated with this robot.
Expand Down
9 changes: 4 additions & 5 deletions test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ type testDriver struct {
Driver
}

func (t *testDriver) Init() bool { return true }
func (t *testDriver) Start() bool { return true }
func (t *testDriver) Halt() bool { return true }
func (t *testDriver) Start() error { return nil }
func (t *testDriver) Halt() error { return nil }

func NewTestDriver(name string, adaptor *testAdaptor) *testDriver {
t := &testDriver{
Expand Down Expand Up @@ -94,8 +93,8 @@ type testAdaptor struct {
Adaptor
}

func (t *testAdaptor) Finalize() bool { return true }
func (t *testAdaptor) Connect() bool { return true }
func (t *testAdaptor) Finalize() error { return nil }
func (t *testAdaptor) Connect() error { return nil }

func NewTestAdaptor(name string) *testAdaptor {
return &testAdaptor{
Expand Down

0 comments on commit 17cbf2c

Please sign in to comment.