From aadb5997d85839f55bc52b3dc2f4cb00b8a8ab9d Mon Sep 17 00:00:00 2001 From: John McCabe Date: Wed, 25 Jul 2018 15:38:34 +0100 Subject: [PATCH] Handle url.Error when vmpooler inaccessible --- commands/menu.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/commands/menu.go b/commands/menu.go index 9f790cc..68d067c 100644 --- a/commands/menu.go +++ b/commands/menu.go @@ -2,6 +2,7 @@ package commands import ( "fmt" + "net/url" "os" "sort" "strings" @@ -45,12 +46,12 @@ func runMenu(cmd *cobra.Command, args []string) { templates, err := vmclient.ListTemplates() if err != nil { - fmt.Println("error getting templates: %s", err) + errorMenu(err) } virtualmachines, err := vmclient.GetAll() if err != nil { - fmt.Println("error getting virtualmachines: %s", err) + errorMenu(err) } plugin := bitbar.New() @@ -225,3 +226,20 @@ func getTemplateOS(template string) string { parts := strings.Split(template, "-") return parts[0] } + +func errorMenu(err error) { + var errMsg string + + switch err.(type) { + case *url.Error: + errMsg = "Unable to connect to VMPooler" + default: + errMsg = fmt.Sprintf("%s ...", err.Error()[:12]) + } + plugin := bitbar.New() + plugin.StatusLine("VMs: ⛔️").Color("red") + menu := plugin.NewSubMenu() + menu.Line(errMsg).CopyToClipboard(err.Error()) + fmt.Print(plugin.Render()) + os.Exit(1) +}