add note and error description (#246)

* add note

* add error description when fail

* typo
pull/252/head
LuttyYang 4 years ago committed by GitHub
parent 5427bcb8eb
commit 02a812cd6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      README.md
  2. 9
      cmd/subcommands/governance.go
  3. 2
      cmd/subcommands/values.go
  4. 1
      go.mod
  5. 2
      go.sum
  6. 20
      pkg/governance/request.go

@ -138,7 +138,7 @@ Check README for details on json file format.
24. New Proposal In Space Of Governance
./hmy governance new-proposal --proposal-yaml=[file path] --key=[key name]
PS: key must first use (hmy keys import-private-key) to import
Yaml example:
Yaml example(time is in UTC timezone):
space: staking-testnet
start: 2020-04-16 21:45:12
end: 2020-04-21 21:45:12

@ -1,6 +1,7 @@
package cmd
import (
"fmt"
"github.com/harmony-one/go-sdk/pkg/governance"
"github.com/harmony-one/go-sdk/pkg/store"
"github.com/harmony-one/harmony/accounts"
@ -85,6 +86,10 @@ func commandNewProposal() (cmd *cobra.Command) {
return err
}
if len(keyStore.Accounts()) <= 0 {
return fmt.Errorf("couldn't find address from the key")
}
account := accounts.Account{Address: keyStore.Accounts()[0].Address}
err = keyStore.Unlock(accounts.Account{Address: keyStore.Accounts()[0].Address}, passphrase)
if err != nil {
@ -118,6 +123,10 @@ func commandVote() (cmd *cobra.Command) {
return err
}
if len(keyStore.Accounts()) <= 0 {
return fmt.Errorf("couldn't find address from the key")
}
account := accounts.Account{Address: keyStore.Accounts()[0].Address}
err = keyStore.Unlock(accounts.Account{Address: keyStore.Accounts()[0].Address}, passphrase)
if err != nil {

@ -116,7 +116,7 @@ Check README for details on json file format.
%s
./hmy governance new-proposal --proposal-yaml=[file path] --key=[key name]
PS: key must first use (hmy keys import-private-key) to import
Yaml example:
Yaml example(time is in UTC timezone):
space: staking-testnet
start: 2020-04-16 21:45:12
end: 2020-04-21 21:45:12

@ -31,6 +31,7 @@ require (
github.com/uber/jaeger-client-go v2.20.1+incompatible // indirect
github.com/uber/jaeger-lib v2.2.0+incompatible // indirect
github.com/valyala/fasthttp v1.2.0
github.com/valyala/fastjson v1.6.3 // indirect
github.com/wangjia184/sortedset v0.0.0-20160527075905-f5d03557ba30 // indirect
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b

@ -1604,6 +1604,8 @@ github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6Kllzaw
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.2.0 h1:dzZJf2IuMiclVjdw0kkT+f9u4YdrapbNyGAN47E/qnk=
github.com/valyala/fasthttp v1.2.0/go.mod h1:4vX61m6KN+xDduDNwXrhIAVZaZaZiQ1luJk8LWSxF3s=
github.com/valyala/fastjson v1.6.3 h1:tAKFnnwmeMGPbwJ7IwxcTPCNr3uIzoIj3/Fh90ra4xc=
github.com/valyala/fastjson v1.6.3/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
github.com/valyala/quicktemplate v1.1.1/go.mod h1:EH+4AkTd43SvgIbQHYu59/cJyxDoOVRUAfrukLPuGJ4=
github.com/valyala/quicktemplate v1.2.0 h1:BaO1nHTkspYzmAjPXj0QiDJxai96tlcZyKcI9dyEGvM=
github.com/valyala/quicktemplate v1.2.0/go.mod h1:EH+4AkTd43SvgIbQHYu59/cJyxDoOVRUAfrukLPuGJ4=

@ -3,6 +3,9 @@ package governance
import (
"bytes"
"encoding/json"
"fmt"
"github.com/valyala/fastjson"
"io"
"net/http"
)
@ -13,7 +16,7 @@ func getAndParse(url governanceApi, data interface{}) error {
}
defer resp.Body.Close()
return json.NewDecoder(resp.Body).Decode(data)
return parseAndUnmarshal(resp, data)
}
func postAndParse(url governanceApi, postData []byte, data interface{}) error {
@ -23,5 +26,18 @@ func postAndParse(url governanceApi, postData []byte, data interface{}) error {
}
defer resp.Body.Close()
return json.NewDecoder(resp.Body).Decode(data)
return parseAndUnmarshal(resp, data)
}
func parseAndUnmarshal(resp *http.Response, data interface{}) error {
bodyData, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if fastjson.GetString(bodyData, "error") != "" {
return fmt.Errorf("error: %s, %s", fastjson.GetString(bodyData, "error"), fastjson.GetString(bodyData, "error_description"))
}
return json.Unmarshal(bodyData, data)
}

Loading…
Cancel
Save