packer/rpc: Allow "error" interfaces to be sent over RPC

This commit is contained in:
Mitchell Hashimoto 2013-05-21 11:58:14 -07:00
parent 8dfe78dd19
commit cc4970d424
2 changed files with 27 additions and 2 deletions

View File

@ -23,13 +23,32 @@ type UiSayArgs struct {
} }
func (u *Ui) Error(format string, a ...interface{}) { func (u *Ui) Error(format string, a ...interface{}) {
u.processArgs(a)
args := &UiSayArgs{format, a} args := &UiSayArgs{format, a}
u.client.Call("Ui.Error", args, new(interface{})) if err := u.client.Call("Ui.Error", args, new(interface{})); err != nil {
panic(err)
}
} }
func (u *Ui) Say(format string, a ...interface{}) { func (u *Ui) Say(format string, a ...interface{}) {
u.processArgs(a)
args := &UiSayArgs{format, a} args := &UiSayArgs{format, a}
u.client.Call("Ui.Say", args, new(interface{})) if err := u.client.Call("Ui.Say", args, new(interface{})); err != nil {
panic(err)
}
}
func (u *Ui) processArgs(a []interface{}) {
// We do some processing to turn certain types into more gob-friendly
// types so that some things that users expect to do just work.
for i, v := range a {
// Turn errors into strings
if err, ok := v.(error); ok {
a[i] = err.Error()
}
}
} }
func (u *UiServer) Error(args *UiSayArgs, reply *interface{}) error { func (u *UiServer) Error(args *UiSayArgs, reply *interface{}) error {

View File

@ -2,6 +2,7 @@ package rpc
import ( import (
"cgl.tideland.biz/asserts" "cgl.tideland.biz/asserts"
"errors"
"net/rpc" "net/rpc"
"testing" "testing"
) )
@ -46,9 +47,14 @@ func TestUiRPC(t *testing.T) {
uiClient := &Ui{client} uiClient := &Ui{client}
// Basic error and say tests
uiClient.Error("format", "arg0", 42) uiClient.Error("format", "arg0", 42)
assert.Equal(ui.errorFormat, "format", "format should be correct") assert.Equal(ui.errorFormat, "format", "format should be correct")
uiClient.Say("format", "arg0", 42) uiClient.Say("format", "arg0", 42)
assert.Equal(ui.sayFormat, "format", "format should be correct") assert.Equal(ui.sayFormat, "format", "format should be correct")
// Test that errors are properly converted to strings
uiClient.Say("format", errors.New("foo"))
assert.Equal(ui.sayVars, []interface{}{"foo"}, "should have correct vars")
} }