From e95e22f0676ff722e31644633b235035e9c1b615 Mon Sep 17 00:00:00 2001 From: Daniel Whitenack Date: Thu, 30 Jun 2016 08:23:07 -0500 Subject: [PATCH] error handling to C --- src/gethdep_test.go | 14 +++++++------- src/library.go | 45 ++++++++++++++++++++++++++++++++++----------- 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/src/gethdep_test.go b/src/gethdep_test.go index 31a330115..9033c3f1b 100644 --- a/src/gethdep_test.go +++ b/src/gethdep_test.go @@ -11,13 +11,6 @@ import ( // unlock that account func TestAccountBindings(t *testing.T) { - // create an account - address, _, err := createAccount("badpassword", ".ethereumtest/keystore") - if err != nil { - fmt.Println(err.Error()) - t.Error("Test failed: could not create account") - } - // start geth node and wait for it to initialize go createAndStartNode(".ethereumtest") time.Sleep(5 * time.Second) @@ -25,6 +18,13 @@ func TestAccountBindings(t *testing.T) { t.Error("Test failed: could not start geth node") } + // create an account + address, _, err := createAccount("badpassword", ".ethereumtest/keystore") + if err != nil { + fmt.Println(err.Error()) + t.Error("Test failed: could not create account") + } + // unlock the created account err = unlockAccount(address, "badpassword", 10) if err != nil { diff --git a/src/library.go b/src/library.go index 98dc5ac67..6a5a04334 100644 --- a/src/library.go +++ b/src/library.go @@ -9,20 +9,27 @@ import ( //export CreateAccount func CreateAccount(password, keydir *C.char) *C.char { + // This is equivalent to creating an account from the command line, // just modified to handle the function arg passing address, pubKey, err := createAccount(C.GoString(password), C.GoString(keydir)) + + var errString string + if err != nil { + fmt.Fprintln(os.Stderr, err) + errString = err.Error() + } else { + errString = "" + } + out := AccountInfo{ Address: address, PubKey: pubKey, - Error: err.Error(), - } - if err != nil { - fmt.Fprintln(os.Stderr, err) + Error: errString, } outBytes, _ := json.Marshal(&out) - return C.CString(string(outBytes)) + return C.CString(string(outBytes)) } //export Login @@ -35,30 +42,46 @@ func Login(address, password *C.char) *C.char { //export UnlockAccount func UnlockAccount(address, password *C.char, seconds int) *C.char { + // This is equivalent to unlocking an account from the command line, // just modified to unlock the account for the currently running geth node // based on the provided arguments err := unlockAccount(C.GoString(address), C.GoString(password), seconds) - out := JSONError{ - Error: err.Error(), - } + + var errString string if err != nil { fmt.Fprintln(os.Stderr, err) + errString = err.Error() + } else { + errString = "" + } + + out := JSONError{ + Error: errString, } outBytes, _ := json.Marshal(&out) + return C.CString(string(outBytes)) } //export StartNode func StartNode(datadir *C.char) *C.char { + // This starts a geth node with the given datadir err := createAndStartNode(C.GoString(datadir)) - out := JSONError{ - Error: err.Error(), - } + + var errString string if err != nil { fmt.Fprintln(os.Stderr, err) + errString = err.Error() + } else { + errString = "" + } + + out := JSONError{ + Error: errString, } outBytes, _ := json.Marshal(&out) + return C.CString(string(outBytes)) }