history: add error sources and stack to json output for inspect

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2025-02-07 11:37:22 +01:00 committed by CrazyMax
parent ccca7c795a
commit 633e8a0881
No known key found for this signature in database
GPG Key ID: ADE44D8C9D44FBE4

View File

@ -116,8 +116,8 @@ type errorOutput struct {
Message string `json:"message,omitempty"` Message string `json:"message,omitempty"`
Name string `json:"name,omitempty"` Name string `json:"name,omitempty"`
Logs []string `json:"logs,omitempty"` Logs []string `json:"logs,omitempty"`
Sources []byte `json:"sources,omitempty"`
statusErr error Stack []byte `json:"stack,omitempty"`
} }
type keyValueOutput struct { type keyValueOutput struct {
@ -314,9 +314,15 @@ workers0:
if err := proto.Unmarshal(dt, &st); err != nil { if err := proto.Unmarshal(dt, &st); err != nil {
return errors.Wrapf(err, "failed to unmarshal external error %s", rec.ExternalError.Digest) 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 var ve *errdefs.VertexError
if errors.As(out.Error.statusErr, &ve) { if errors.As(retErr, &ve) {
dgst, err := digest.Parse(ve.Vertex.Digest) dgst, err := digest.Parse(ve.Vertex.Digest)
if err != nil { if err != nil {
return errors.Wrapf(err, "failed to parse vertex digest %s", ve.Vertex.Digest) return errors.Wrapf(err, "failed to parse vertex digest %s", ve.Vertex.Digest)
@ -328,6 +334,7 @@ workers0:
out.Error.Name = name out.Error.Name = name
out.Error.Logs = logs out.Error.Logs = logs
} }
out.Error.Stack = []byte(fmt.Sprintf("%+v", stack.Formatter(retErr)))
} }
} }
@ -566,10 +573,9 @@ workers0:
} }
if out.Error != nil { if out.Error != nil {
for _, s := range errdefs.Sources(out.Error.statusErr) { if out.Error.Sources != nil {
s.Print(dockerCli.Out()) fmt.Fprint(dockerCli.Out(), string(out.Error.Sources))
} }
fmt.Fprintln(dockerCli.Out())
if len(out.Error.Logs) > 0 { if len(out.Error.Logs) > 0 {
fmt.Fprintln(dockerCli.Out(), "Logs:") fmt.Fprintln(dockerCli.Out(), "Logs:")
fmt.Fprintf(dockerCli.Out(), "> => %s:\n", out.Error.Name) fmt.Fprintf(dockerCli.Out(), "> => %s:\n", out.Error.Name)
@ -578,10 +584,12 @@ workers0:
} }
fmt.Fprintln(dockerCli.Out()) fmt.Fprintln(dockerCli.Out())
} }
if debug.IsEnabled() { if len(out.Error.Stack) > 0 {
fmt.Fprintf(dockerCli.Out(), "\n%+v\n", stack.Formatter(out.Error.statusErr)) if debug.IsEnabled() {
} else if len(stack.Traces(out.Error.statusErr)) > 0 { fmt.Fprintf(dockerCli.Out(), "\n%s\n", out.Error.Stack)
fmt.Fprintf(dockerCli.Out(), "Enable --debug to see stack traces for error\n") } else {
fmt.Fprintf(dockerCli.Out(), "Enable --debug to see stack traces for error\n")
}
} }
} }