Skip to content

Commit

Permalink
Improve error message of buildrun failure
Browse files Browse the repository at this point in the history
Change format so that multiple errors are better to read.

Include buildrun status in error message.

Remove unnecessary linebreaks from container logs.
  • Loading branch information
HeavyWombat committed Mar 9, 2021
1 parent f8757f2 commit 3cb494c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
42 changes: 25 additions & 17 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
10 changes: 6 additions & 4 deletions internal/load/kubeops.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"encoding/json"
"fmt"
"hash/fnv"
"strings"
"time"

buildv1alpha1 "github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
Expand All @@ -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"
Expand Down Expand Up @@ -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)

Expand All @@ -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())
}
}
}
Expand Down

0 comments on commit 3cb494c

Please sign in to comment.