Skip to content

Commit 1cc0e2c

Browse files
committed
[FAB-4855]Print error if MSP config folder missing
Currently on peer start if MSP configuration folder is misconfigured or missing peer simply crashes with panic rather than providing an error message, this commit add check to see if MSP folders actually exists and provides error message instead of panicing crashing the peer + UT. Change-Id: Ic0e133603e7ec924225b6bfb4b08ebceaced5a55 Signed-off-by: Artem Barger <[email protected]>
1 parent b4eef8e commit 1cc0e2c

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

peer/common/common.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package common
1818

1919
import (
2020
"fmt"
21+
"os"
2122

2223
"github.com/hyperledger/fabric/bccsp/factory"
2324
"github.com/hyperledger/fabric/common/configtx"
@@ -33,7 +34,7 @@ import (
3334
pcommon "github.com/hyperledger/fabric/protos/common"
3435
pb "github.com/hyperledger/fabric/protos/peer"
3536
putils "github.com/hyperledger/fabric/protos/utils"
36-
logging "github.com/op/go-logging"
37+
"github.com/op/go-logging"
3738
"github.com/spf13/viper"
3839
"golang.org/x/net/context"
3940
)
@@ -85,16 +86,24 @@ func InitConfig(cmdRoot string) error {
8586

8687
//InitCrypto initializes crypto for this peer
8788
func InitCrypto(mspMgrConfigDir string, localMSPID string) error {
89+
var err error
90+
// Check whenever msp folder exists
91+
_, err = os.Stat(mspMgrConfigDir)
92+
if os.IsNotExist(err) {
93+
// No need to try to load MSP from folder which is not available
94+
return fmt.Errorf("cannot init crypto, missing %s folder", mspMgrConfigDir)
95+
}
96+
8897
// Init the BCCSP
8998
var bccspConfig *factory.FactoryOpts
90-
err := viperutil.EnhancedExactUnmarshalKey("peer.BCCSP", &bccspConfig)
99+
err = viperutil.EnhancedExactUnmarshalKey("peer.BCCSP", &bccspConfig)
91100
if err != nil {
92-
return fmt.Errorf("Could not parse YAML config [%s]", err)
101+
return fmt.Errorf("could not parse YAML config [%s]", err)
93102
}
94103

95104
err = mspmgmt.LoadLocalMsp(mspMgrConfigDir, bccspConfig, localMSPID)
96105
if err != nil {
97-
return fmt.Errorf("Error when setting up MSP from directory %s: err %s", mspMgrConfigDir, err)
106+
return fmt.Errorf("error when setting up MSP from directory %s: err %s", mspMgrConfigDir, err)
98107
}
99108

100109
return nil

peer/common/common_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ SPDX-License-Identifier: Apache-2.0
77
package common_test
88

99
import (
10+
"fmt"
11+
"os"
1012
"testing"
1113

14+
"github.com/hyperledger/fabric/common/util"
1215
"github.com/hyperledger/fabric/core/config"
1316
"github.com/hyperledger/fabric/msp"
1417
"github.com/hyperledger/fabric/peer/common"
@@ -50,6 +53,13 @@ func TestInitConfig(t *testing.T) {
5053
}
5154
}
5255

56+
func TestINitCryptoMissingDir(t *testing.T) {
57+
dir := os.TempDir() + "/" + util.GenerateUUID()
58+
err := common.InitCrypto(dir, "DEFAULT")
59+
assert.Error(t, err, "Should be able to initialize crypto with non-existing directory")
60+
assert.Contains(t, err.Error(), fmt.Sprintf("missing %s folder", dir))
61+
}
62+
5363
func TestInitCrypto(t *testing.T) {
5464

5565
mspConfigPath, err := config.GetDevMspDir()

peer/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ func main() {
110110
var mspID = viper.GetString("peer.localMspId")
111111
err = common.InitCrypto(mspMgrConfigDir, mspID)
112112
if err != nil { // Handle errors reading the config file
113-
panic(err.Error())
113+
logger.Errorf("Cannot run peer because %s", err.Error())
114+
os.Exit(1)
114115
}
115116
// On failure Cobra prints the usage message and error string, so we only
116117
// need to exit with a non-0 status

0 commit comments

Comments
 (0)