Unwrap grpc errors to make nicer user facing errors

This commit is contained in:
Kristoffer Dalby 2021-11-07 10:15:32 +00:00
parent b11acad1c9
commit d47b83f80b
4 changed files with 18 additions and 14 deletions

View file

@ -6,6 +6,7 @@ import (
v1 "github.com/juanfont/headscale/gen/go/headscale/v1" v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"google.golang.org/grpc/status"
) )
func init() { func init() {
@ -80,7 +81,7 @@ var createNodeCmd = &cobra.Command{
response, err := client.DebugCreateMachine(ctx, request) response, err := client.DebugCreateMachine(ctx, request)
if err != nil { if err != nil {
ErrorOutput(err, fmt.Sprintf("Cannot create machine: %s", err), output) ErrorOutput(err, fmt.Sprintf("Cannot create machine: %s", status.Convert(err).Message()), output)
return return
} }

View file

@ -7,6 +7,7 @@ import (
"github.com/pterm/pterm" "github.com/pterm/pterm"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"google.golang.org/grpc/status"
) )
func init() { func init() {
@ -47,7 +48,7 @@ var createNamespaceCmd = &cobra.Command{
log.Trace().Interface("request", request).Msg("Sending CreateNamespace request") log.Trace().Interface("request", request).Msg("Sending CreateNamespace request")
response, err := client.CreateNamespace(ctx, request) response, err := client.CreateNamespace(ctx, request)
if err != nil { if err != nil {
ErrorOutput(err, fmt.Sprintf("Cannot create namespace: %s", err), output) ErrorOutput(err, fmt.Sprintf("Cannot create namespace: %s", status.Convert(err).Message()), output)
return return
} }
@ -77,7 +78,7 @@ var destroyNamespaceCmd = &cobra.Command{
response, err := client.DeleteNamespace(ctx, request) response, err := client.DeleteNamespace(ctx, request)
if err != nil { if err != nil {
ErrorOutput(err, fmt.Sprintf("Cannot destroy namespace: %s", err), output) ErrorOutput(err, fmt.Sprintf("Cannot destroy namespace: %s", status.Convert(err).Message()), output)
return return
} }
@ -99,7 +100,7 @@ var listNamespacesCmd = &cobra.Command{
response, err := client.ListNamespaces(ctx, request) response, err := client.ListNamespaces(ctx, request)
if err != nil { if err != nil {
ErrorOutput(err, fmt.Sprintf("Cannot get namespaces: %s", err), output) ErrorOutput(err, fmt.Sprintf("Cannot get namespaces: %s", status.Convert(err).Message()), output)
return return
} }
@ -150,7 +151,7 @@ var renameNamespaceCmd = &cobra.Command{
response, err := client.RenameNamespace(ctx, request) response, err := client.RenameNamespace(ctx, request)
if err != nil { if err != nil {
ErrorOutput(err, fmt.Sprintf("Cannot rename namespace: %s", err), output) ErrorOutput(err, fmt.Sprintf("Cannot rename namespace: %s", status.Convert(err).Message()), output)
return return
} }

View file

@ -10,6 +10,7 @@ import (
v1 "github.com/juanfont/headscale/gen/go/headscale/v1" v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
"github.com/pterm/pterm" "github.com/pterm/pterm"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"google.golang.org/grpc/status"
"tailscale.com/tailcfg" "tailscale.com/tailcfg"
"tailscale.com/types/wgkey" "tailscale.com/types/wgkey"
) )
@ -125,7 +126,7 @@ var listNodesCmd = &cobra.Command{
response, err := client.ListMachines(ctx, request) response, err := client.ListMachines(ctx, request)
if err != nil { if err != nil {
ErrorOutput(err, fmt.Sprintf("Cannot get nodes: %s", err), output) ErrorOutput(err, fmt.Sprintf("Cannot get nodes: %s", status.Convert(err).Message()), output)
return return
} }
@ -170,7 +171,7 @@ var deleteNodeCmd = &cobra.Command{
getResponse, err := client.GetMachine(ctx, getRequest) getResponse, err := client.GetMachine(ctx, getRequest)
if err != nil { if err != nil {
ErrorOutput(err, fmt.Sprintf("Error getting node node: %s", err), output) ErrorOutput(err, fmt.Sprintf("Error getting node node: %s", status.Convert(err).Message()), output)
return return
} }
@ -197,7 +198,7 @@ var deleteNodeCmd = &cobra.Command{
return return
} }
if err != nil { if err != nil {
ErrorOutput(err, fmt.Sprintf("Error deleting node: %s", err), output) ErrorOutput(err, fmt.Sprintf("Error deleting node: %s", status.Convert(err).Message()), output)
return return
} }
SuccessOutput(map[string]string{"Result": "Node deleted"}, "Node deleted", output) SuccessOutput(map[string]string{"Result": "Node deleted"}, "Node deleted", output)
@ -234,7 +235,7 @@ func sharingWorker(
machineResponse, err := client.GetMachine(ctx, machineRequest) machineResponse, err := client.GetMachine(ctx, machineRequest)
if err != nil { if err != nil {
ErrorOutput(err, fmt.Sprintf("Error getting node node: %s", err), output) ErrorOutput(err, fmt.Sprintf("Error getting node node: %s", status.Convert(err).Message()), output)
return "", nil, nil, err return "", nil, nil, err
} }
@ -244,7 +245,7 @@ func sharingWorker(
namespaceResponse, err := client.GetNamespace(ctx, namespaceRequest) namespaceResponse, err := client.GetNamespace(ctx, namespaceRequest)
if err != nil { if err != nil {
ErrorOutput(err, fmt.Sprintf("Error getting node node: %s", err), output) ErrorOutput(err, fmt.Sprintf("Error getting node node: %s", status.Convert(err).Message()), output)
return "", nil, nil, err return "", nil, nil, err
} }
@ -272,7 +273,7 @@ var shareMachineCmd = &cobra.Command{
response, err := client.ShareMachine(ctx, request) response, err := client.ShareMachine(ctx, request)
if err != nil { if err != nil {
ErrorOutput(err, fmt.Sprintf("Error sharing node: %s", err), output) ErrorOutput(err, fmt.Sprintf("Error sharing node: %s", status.Convert(err).Message()), output)
return return
} }
@ -301,7 +302,7 @@ var unshareMachineCmd = &cobra.Command{
response, err := client.UnshareMachine(ctx, request) response, err := client.UnshareMachine(ctx, request)
if err != nil { if err != nil {
ErrorOutput(err, fmt.Sprintf("Error unsharing node: %s", err), output) ErrorOutput(err, fmt.Sprintf("Error unsharing node: %s", status.Convert(err).Message()), output)
return return
} }

View file

@ -8,6 +8,7 @@ import (
v1 "github.com/juanfont/headscale/gen/go/headscale/v1" v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
"github.com/pterm/pterm" "github.com/pterm/pterm"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"google.golang.org/grpc/status"
) )
func init() { func init() {
@ -59,7 +60,7 @@ var listRoutesCmd = &cobra.Command{
response, err := client.GetMachineRoute(ctx, request) response, err := client.GetMachineRoute(ctx, request)
if err != nil { if err != nil {
ErrorOutput(err, fmt.Sprintf("Cannot get nodes: %s", err), output) ErrorOutput(err, fmt.Sprintf("Cannot get nodes: %s", status.Convert(err).Message()), output)
return return
} }
@ -115,7 +116,7 @@ omit the route you do not want to enable.
response, err := client.EnableMachineRoutes(ctx, request) response, err := client.EnableMachineRoutes(ctx, request)
if err != nil { if err != nil {
ErrorOutput(err, fmt.Sprintf("Cannot register machine: %s\n", err), output) ErrorOutput(err, fmt.Sprintf("Cannot register machine: %s\n", status.Convert(err).Message()), output)
return return
} }