From 633e8a0881e409ee7e999412c64682ed53d7e0b1 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Fri, 7 Feb 2025 11:37:22 +0100 Subject: [PATCH] history: add error sources and stack to json output for inspect Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> --- commands/history/inspect.go | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/commands/history/inspect.go b/commands/history/inspect.go index 29b46769..d18be481 100644 --- a/commands/history/inspect.go +++ b/commands/history/inspect.go @@ -116,8 +116,8 @@ type errorOutput struct { Message string `json:"message,omitempty"` Name string `json:"name,omitempty"` Logs []string `json:"logs,omitempty"` - - statusErr error + Sources []byte `json:"sources,omitempty"` + Stack []byte `json:"stack,omitempty"` } type keyValueOutput struct { @@ -314,9 +314,15 @@ workers0: if err := proto.Unmarshal(dt, &st); err != nil { return errors.Wrapf(err, "failed to unmarshal external error %s", rec.ExternalError.Digest) } - out.Error.statusErr = grpcerrors.FromGRPC(status.ErrorProto(&st)) + retErr := grpcerrors.FromGRPC(status.ErrorProto(&st)) + var errsources bytes.Buffer + for _, s := range errdefs.Sources(retErr) { + s.Print(&errsources) + errsources.WriteString("\n") + } + out.Error.Sources = errsources.Bytes() var ve *errdefs.VertexError - if errors.As(out.Error.statusErr, &ve) { + if errors.As(retErr, &ve) { dgst, err := digest.Parse(ve.Vertex.Digest) if err != nil { return errors.Wrapf(err, "failed to parse vertex digest %s", ve.Vertex.Digest) @@ -328,6 +334,7 @@ workers0: out.Error.Name = name out.Error.Logs = logs } + out.Error.Stack = []byte(fmt.Sprintf("%+v", stack.Formatter(retErr))) } } @@ -566,10 +573,9 @@ workers0: } if out.Error != nil { - for _, s := range errdefs.Sources(out.Error.statusErr) { - s.Print(dockerCli.Out()) + if out.Error.Sources != nil { + fmt.Fprint(dockerCli.Out(), string(out.Error.Sources)) } - fmt.Fprintln(dockerCli.Out()) if len(out.Error.Logs) > 0 { fmt.Fprintln(dockerCli.Out(), "Logs:") fmt.Fprintf(dockerCli.Out(), "> => %s:\n", out.Error.Name) @@ -578,10 +584,12 @@ workers0: } fmt.Fprintln(dockerCli.Out()) } - if debug.IsEnabled() { - fmt.Fprintf(dockerCli.Out(), "\n%+v\n", stack.Formatter(out.Error.statusErr)) - } else if len(stack.Traces(out.Error.statusErr)) > 0 { - fmt.Fprintf(dockerCli.Out(), "Enable --debug to see stack traces for error\n") + if len(out.Error.Stack) > 0 { + if debug.IsEnabled() { + fmt.Fprintf(dockerCli.Out(), "\n%s\n", out.Error.Stack) + } else { + fmt.Fprintf(dockerCli.Out(), "Enable --debug to see stack traces for error\n") + } } }