From 3cb494c43208257b1a8e5489ba5cb7d71d07f66d Mon Sep 17 00:00:00 2001 From: Matthias Diester Date: Tue, 9 Mar 2021 16:56:53 +0100 Subject: [PATCH] Improve error message of buildrun failure Change format so that multiple errors are better to read. Include buildrun status in error message. Remove unnecessary linebreaks from container logs. --- internal/cmd/root.go | 42 ++++++++++++++++++++++++---------------- internal/load/kubeops.go | 10 ++++++---- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/internal/cmd/root.go b/internal/cmd/root.go index 58fc1f40..f9bf4738 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -43,28 +43,36 @@ var rootCmd = &cobra.Command{ // This is called by main.main(). It only needs to happen once to the rootCmd. func Execute() { if err := rootCmd.Execute(); err != nil { - var headline = "Error occurred" - var code = 1 - var buf bytes.Buffer + fmt.Fprint(os.Stderr, readableError(err)) + os.Exit(1) + } +} - switch terr := err.(type) { - case wrap.ContextError: - headline = fmt.Sprintf("Error: %s", terr.Context()) - buf.WriteString(terr.Cause().Error()) +func readableError(err error) string { + var headline = "Error occurred" + var buf bytes.Buffer - default: - buf.WriteString(terr.Error()) - } + switch terr := err.(type) { + case wrap.ContextError: + headline = fmt.Sprintf("Error: %s", terr.Context()) + buf.WriteString(terr.Cause().Error()) - neat.Box( - os.Stderr, - headline, - &buf, - neat.HeadlineColor(bunt.Coral), - ) + case wrap.ListOfErrors: + headline = "Multiple errors occurred" + for _, e := range terr.Errors() { + buf.WriteString(readableError(e)) + } - os.Exit(code) + default: + buf.WriteString(terr.Error()) } + + return neat.ContentBox( + headline, + buf.String(), + neat.HeadlineColor(bunt.Coral), + neat.NoLineWrap(), + ) } func init() { diff --git a/internal/load/kubeops.go b/internal/load/kubeops.go index f81d7a56..431e70c9 100644 --- a/internal/load/kubeops.go +++ b/internal/load/kubeops.go @@ -23,7 +23,6 @@ import ( "encoding/json" "fmt" "hash/fnv" - "strings" "time" buildv1alpha1 "github.com/shipwright-io/build/pkg/apis/build/v1alpha1" @@ -32,6 +31,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "github.com/gonvenience/bunt" + "github.com/gonvenience/neat" "github.com/gonvenience/wrap" "github.com/lucasb-eyer/go-colorful" "k8s.io/apimachinery/pkg/api/errors" @@ -295,6 +295,10 @@ func buildRunError(kubeAccess KubeAccess, buildRun buildv1alpha1.BuildRun) error ) } + status, _ := neat.ToYAMLString(buildRun.Status) + bunt.Fprintf(&buf, "*BuildRun Status*\n%s\n\n", status) + + bunt.Fprintf(&buf, "*Pod container logs*\n") for _, container := range append(taskRunPod.Spec.InitContainers, taskRunPod.Spec.Containers...) { containerName := colorise(container.Name) @@ -314,9 +318,7 @@ func buildRunError(kubeAccess KubeAccess, buildRun buildv1alpha1.BuildRun) error var scanner = bufio.NewScanner(reader) for scanner.Scan() { - for _, line := range strings.Split(scanner.Text(), "\n") { - fmt.Fprintf(&buf, "%s %s\n", containerName, line) - } + fmt.Fprintf(&buf, "%s %s\n", containerName, scanner.Text()) } } }