Skip to content

Commit

Permalink
Add error check and improve msg
Browse files Browse the repository at this point in the history
The patchset checks the error when doing yaml marshal, and improves
the message when error happens.

Change-Id: I40fb7dbe930bcdc6563078daab50f4f4564c3381
Signed-off-by: Baohua Yang <yangbaohua@gmail.com>
Signed-off-by: Baohua Yang <baohua.yang@oracle.com>
  • Loading branch information
yeasy authored and denyeart committed Oct 2, 2023
1 parent dcf07a8 commit 745b96c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
20 changes: 12 additions & 8 deletions cmd/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,28 @@ func (c Config) ToFile(file string) error {
if err := validateConfig(c); err != nil {
return errors.Wrap(err, "config isn't valid")
}
b, _ := yaml.Marshal(c)
b, err := yaml.Marshal(c)
if err != nil {
return errors.Wrap(err, "failed to marshal config")
}
if err := os.WriteFile(file, b, 0o600); err != nil {
return errors.Errorf("failed writing file %s: %v", file, err)
}
return nil
}

func validateConfig(conf Config) error {
nonEmptyStrings := []string{
conf.SignerConfig.MSPID,
conf.SignerConfig.IdentityPath,
conf.SignerConfig.KeyPath,
nonEmptyElems := map[string]string{
"MSPID": conf.SignerConfig.MSPID,
"IdentityPath": conf.SignerConfig.IdentityPath,
"KeyPath": conf.SignerConfig.KeyPath,
}

for _, s := range nonEmptyStrings {
if s == "" {
return errors.New("empty string that is mandatory")
for key, value := range nonEmptyElems {
if value == "" {
return errors.Errorf("%s is mandatory and cannot be empty", key)
}
}

return nil
}
2 changes: 1 addition & 1 deletion cmd/discover/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ func TestMissingArguments(t *testing.T) {
process, err := Start(cmd, nil, nil)
gt.Expect(err).NotTo(HaveOccurred())
gt.Eventually(process, 5*time.Second).Should(Exit(1))
gt.Expect(process.Err).To(gbytes.Say("empty string that is mandatory"))
gt.Expect(process.Err).To(gbytes.Say("IdentityPath|KeyPath is mandatory and cannot be empty"))
}

0 comments on commit 745b96c

Please sign in to comment.