Commit c580faa1 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer/rpc: error interface wrapper to RPC errors around

parent 6d4136c5
...@@ -47,7 +47,11 @@ func (Command) Run(env packer.Environment, args []string) int { ...@@ -47,7 +47,11 @@ func (Command) Run(env packer.Environment, args []string) int {
// Prepare all the builds // Prepare all the builds
for _, b := range builds { for _, b := range builds {
log.Printf("Preparing build: %s\n", b.Name()) log.Printf("Preparing build: %s\n", b.Name())
b.Prepare() err := b.Prepare()
if err != nil {
env.Ui().Error("%s\n", err)
return 1
}
} }
env.Ui().Say("YAY!\n") env.Ui().Say("YAY!\n")
......
...@@ -91,7 +91,7 @@ func (c *client) Start() (address string, err error) { ...@@ -91,7 +91,7 @@ func (c *client) Start() (address string, err error) {
// Start goroutine to wait for process to exit // Start goroutine to wait for process to exit
go func() { go func() {
c.cmd.Wait() c.cmd.Wait()
log.Println("plugin process exited") log.Printf("%s: plugin process exited\n", c.cmd.Path)
c.exited = true c.exited = true
}() }()
......
...@@ -30,7 +30,11 @@ func Builder(client *rpc.Client) *builder { ...@@ -30,7 +30,11 @@ func Builder(client *rpc.Client) *builder {
} }
func (b *builder) Prepare(config interface{}) (err error) { func (b *builder) Prepare(config interface{}) (err error) {
b.client.Call("Builder.Prepare", &BuilderPrepareArgs{config}, &err) cerr := b.client.Call("Builder.Prepare", &BuilderPrepareArgs{config}, &err)
if cerr != nil {
err = cerr
}
return return
} }
...@@ -46,7 +50,11 @@ func (b *builder) Run(build packer.Build, ui packer.Ui) { ...@@ -46,7 +50,11 @@ func (b *builder) Run(build packer.Build, ui packer.Ui) {
} }
func (b *BuilderServer) Prepare(args *BuilderPrepareArgs, reply *error) error { func (b *BuilderServer) Prepare(args *BuilderPrepareArgs, reply *error) error {
*reply = b.builder.Prepare(args.Config) err := b.builder.Prepare(args.Config)
if err != nil {
*reply = NewBasicError(err)
}
return nil return nil
} }
......
package rpc
// This is a type that wraps error types so that they can be messaged
// across RPC channels. Since "error" is an interface, we can't always
// gob-encode the underlying structure. This is a valid error interface
// implementer that we will push across.
type BasicError struct {
Message string
}
func NewBasicError(err error) *BasicError {
return &BasicError{err.Error()}
}
func (e *BasicError) Error() string {
return e.Message
}
package rpc
import (
"cgl.tideland.biz/asserts"
"errors"
"testing"
)
func TestBasicError_ImplementsError(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
var r error
e := &BasicError{""}
assert.Implementor(e, &r, "should be an error")
}
func TestBasicError_MatchesMessage(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
err := errors.New("foo")
wrapped := NewBasicError(err)
assert.Equal(wrapped.Error(), err.Error(), "should have the same error")
}
package rpc
import "encoding/gob"
func init() {
gob.Register(new(map[string]interface{}))
gob.Register(new(BasicError))
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment