Commit cc4970d4 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

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

parent 8dfe78dd
...@@ -23,13 +23,32 @@ type UiSayArgs struct { ...@@ -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 {
......
...@@ -2,6 +2,7 @@ package rpc ...@@ -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) { ...@@ -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")
} }
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