From b70493b83faa7598701b4804a25ec18cca7a4217 Mon Sep 17 00:00:00 2001 From: Enda Phelan Date: Fri, 11 Dec 2020 10:29:12 +0000 Subject: [PATCH] fix: display API error reason --- .vscode/launch.json | 12 ++++++++++++ cmd/rhoas/main.go | 23 +++++++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index a5e40d406..f28e013ab 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -51,6 +51,18 @@ "kafka", "list" ] + }, + { + "name": "Describe Kafka", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "${workspaceFolder}/cmd/rhoas", + "env": {}, + "args": [ + "kafka", + "describe" + ] } ] } \ No newline at end of file diff --git a/cmd/rhoas/main.go b/cmd/rhoas/main.go index 7a71b52e2..0ecf0a143 100644 --- a/cmd/rhoas/main.go +++ b/cmd/rhoas/main.go @@ -1,9 +1,12 @@ package main import ( + "errors" "fmt" "os" + "github.com/bf2fc6cc711aee1a0c2a/cli/pkg/api/managedservices" + "github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmd/root" "github.com/bf2fc6cc711aee1a0c2a/cli/pkg/config" "github.com/bf2fc6cc711aee1a0c2a/cli/pkg/version" @@ -26,9 +29,25 @@ func main() { generateDocumentation(rootCmd) } - if err := rootCmd.Execute(); err != nil { - fmt.Fprintf(os.Stderr, "Error: %v\n", err) + err := rootCmd.Execute() + if err == nil { + return + } + + // Attempt to unwrap the descriptive API error message + var apiError managedservices.GenericOpenAPIError + if ok := errors.As(err, &apiError); ok { + errModel := apiError.Model() + + e, ok := errModel.(managedservices.Error) + if ok { + fmt.Fprintf(os.Stderr, "Error: %v\n", e.Reason) + os.Exit(1) + } } + + fmt.Fprintf(os.Stderr, "Error: %v\n", err) + os.Exit(1) } func initConfig() {