@@ -437,34 +437,36 @@ func (a *Application) init() {
437437 }
438438}
439439
440- func (a * Application ) initApiConnection (c * cli.Context ) ( err error ) {
440+ func (a * Application ) initApiConnection (c * cli.Context ) error {
441441 apiAddr := c .String ("api_addr" )
442- var addr string
443- defer func () {
444- if err != nil {
445- return
446- }
447- a .api = apiclient .New (addr )
448- _ , err2 := a .api .PeerInfo ()
449- if err2 != nil {
450- err = fmt .Errorf ("could not access api on address %s: %v" , addr , err2 )
451- }
452- }()
453442 if apiAddr != "" {
454- addr = apiAddr
455- return nil
443+ return a .initApiFromAddr (apiAddr )
456444 }
457- conf , err := config .LoadConfig (eventbus .NewBus ())
458- if err != nil {
459- a .logger .Errorf ("could not load config, use default api_addr (%s), error: %v" , defaultApiAddr , err )
460- addr = defaultApiAddr
445+
446+ conf , errConfig := config .LoadConfig (eventbus .NewBus ())
447+ if errConfig == nil {
448+ return a .initApiFromAddr (conf .HttpListenAddress )
449+ }
450+
451+ errDefault := a .initApiFromAddr (defaultApiAddr )
452+ if errDefault == nil {
461453 return nil
462454 }
463- addr = conf .HttpListenAddress
464- if addr == "" {
465- return errors .New ("httpListenAddress from config is empty" )
455+
456+ a .logger .Errorf ("could not load config file, error: %v" , errConfig )
457+ a .logger .Errorf ("could not connect to default api_addr (%s), error: %v" , defaultApiAddr , errDefault )
458+
459+ return errors .New ("no connection to api server" )
460+ }
461+
462+ func (a * Application ) initApiFromAddr (addr string ) error {
463+ api := apiclient .New (addr )
464+ _ , err := api .PeerInfo ()
465+ if err != nil {
466+ return fmt .Errorf ("could not access api on address %s: %v" , addr , err )
466467 }
467468
469+ a .api = api
468470 return nil
469471}
470472
0 commit comments