From 6035207f2e4e00a82952cd130e96fb1e478ef110 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Thu, 12 Jan 2023 13:02:32 -0500 Subject: [PATCH] improved self-service error messages for zrok invite and zrok enable (#145) --- cmd/zrok/enable.go | 12 +++++++++++- cmd/zrok/invite.go | 40 ++++++++++++++++++++++++---------------- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/cmd/zrok/enable.go b/cmd/zrok/enable.go index 043895d4..2fb3f100 100644 --- a/cmd/zrok/enable.go +++ b/cmd/zrok/enable.go @@ -57,7 +57,8 @@ func (cmd *enableCommand) run(_ *cobra.Command, args []string) { } zrok, err := zrd.Client() if err != nil { - panic(err) + cmd.endpointError(zrd.ApiEndpoint()) + tui.Error("error creating service client", err) } auth := httptransport.APIKeyAuth("X-TOKEN", "header", token) req := environment.NewEnableParams() @@ -87,6 +88,7 @@ func (cmd *enableCommand) run(_ *cobra.Command, args []string) { prg.Send(fmt.Sprintf("the zrok service returned an error: %v", err)) prg.Quit() <-done + cmd.endpointError(zrd.ApiEndpoint()) os.Exit(1) } prg.Send("writing the environment details...") @@ -110,6 +112,14 @@ func (cmd *enableCommand) run(_ *cobra.Command, args []string) { <-done } +func (cmd *enableCommand) endpointError(apiEndpoint, _ string) { + fmt.Printf("%v\n\n", tui.ErrorStyle.Render("there was a problem enabling your environment!")) + fmt.Printf("you are trying to use the zrok service at: %v\n\n", tui.CodeStyle.Render(apiEndpoint)) + fmt.Printf("you can change your zrok service endpoint using this command:\n\n") + fmt.Printf("%v\n\n", tui.CodeStyle.Render("$ zrok config set apiEndpoint ")) + fmt.Printf("(where newEndpoint is something like: %v)\n\n", tui.CodeStyle.Render("https://some.zrok.io")) +} + func getHost() (string, string, error) { info, err := host.Info() if err != nil { diff --git a/cmd/zrok/invite.go b/cmd/zrok/invite.go index 49628980..1d714a43 100644 --- a/cmd/zrok/invite.go +++ b/cmd/zrok/invite.go @@ -44,6 +44,20 @@ func newInviteCommand() *inviteCommand { } func (cmd *inviteCommand) run(_ *cobra.Command, _ []string) { + zrd, err := zrokdir.Load() + if err != nil { + tui.Error("error loading zrokdir", err) + } + + zrok, err := zrd.Client() + if err != nil { + if !panicInstead { + cmd.endpointError(zrd.ApiEndpoint()) + tui.Error("error creating zrok api client", err) + } + panic(err) + } + if _, err := tea.NewProgram(&cmd.tui).Run(); err != nil { tui.Error("unable to run interface", err) os.Exit(1) @@ -51,18 +65,6 @@ func (cmd *inviteCommand) run(_ *cobra.Command, _ []string) { if cmd.tui.done { email := cmd.tui.inputs[0].Value() - zrd, err := zrokdir.Load() - if err != nil { - tui.Error("error loading zrokdir", err) - } - - zrok, err := zrd.Client() - if err != nil { - if !panicInstead { - tui.Error("error creating zrok api client", err) - } - panic(err) - } req := account.NewInviteParams() req.Body = &rest_model_zrok.InviteRequest{ Email: email, @@ -70,16 +72,22 @@ func (cmd *inviteCommand) run(_ *cobra.Command, _ []string) { } _, err = zrok.Account.Invite(req) if err != nil { - if !panicInstead { - tui.Error("error creating invitation", err) - } - panic(err) + cmd.endpointError(zrd.ApiEndpoint()) + tui.Error("error creating invitation", err) } fmt.Printf("invitation sent to '%v'!\n", email) } } +func (cmd *inviteCommand) endpointError(apiEndpoint, _ string) { + fmt.Printf("%v\n\n", tui.ErrorStyle.Render("there was a problem creating an invitation!")) + fmt.Printf("you are trying to use the zrok service at: %v\n\n", tui.CodeStyle.Render(apiEndpoint)) + fmt.Printf("you can change your zrok service endpoint using this command:\n\n") + fmt.Printf("%v\n\n", tui.CodeStyle.Render("$ zrok config set apiEndpoint ")) + fmt.Printf("(where newEndpoint is something like: %v)\n\n", tui.CodeStyle.Render("https://some.zrok.io")) +} + type inviteTui struct { focusIndex int msg string