packer/rpc: error interface wrapper to RPC errors around
This commit is contained in:
parent
6d4136c5ab
commit
c580faa1a5
@ -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) {
|
|||||||
// 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 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
17
packer/rpc/error.go
Normal file
17
packer/rpc/error.go
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
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
|
||||||
|
}
|
25
packer/rpc/error_test.go
Normal file
25
packer/rpc/error_test.go
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
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")
|
||||||
|
}
|
8
packer/rpc/init.go
Normal file
8
packer/rpc/init.go
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package rpc
|
||||||
|
|
||||||
|
import "encoding/gob"
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
gob.Register(new(map[string]interface{}))
|
||||||
|
gob.Register(new(BasicError))
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user