Skip to content

Commit 633e8a0

Browse files
committed
history: add error sources and stack to json output for inspect
Signed-off-by: CrazyMax <[email protected]>
1 parent ccca7c7 commit 633e8a0

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

commands/history/inspect.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ type errorOutput struct {
116116
Message string `json:"message,omitempty"`
117117
Name string `json:"name,omitempty"`
118118
Logs []string `json:"logs,omitempty"`
119-
120-
statusErr error
119+
Sources []byte `json:"sources,omitempty"`
120+
Stack []byte `json:"stack,omitempty"`
121121
}
122122

123123
type keyValueOutput struct {
@@ -314,9 +314,15 @@ workers0:
314314
if err := proto.Unmarshal(dt, &st); err != nil {
315315
return errors.Wrapf(err, "failed to unmarshal external error %s", rec.ExternalError.Digest)
316316
}
317-
out.Error.statusErr = grpcerrors.FromGRPC(status.ErrorProto(&st))
317+
retErr := grpcerrors.FromGRPC(status.ErrorProto(&st))
318+
var errsources bytes.Buffer
319+
for _, s := range errdefs.Sources(retErr) {
320+
s.Print(&errsources)
321+
errsources.WriteString("\n")
322+
}
323+
out.Error.Sources = errsources.Bytes()
318324
var ve *errdefs.VertexError
319-
if errors.As(out.Error.statusErr, &ve) {
325+
if errors.As(retErr, &ve) {
320326
dgst, err := digest.Parse(ve.Vertex.Digest)
321327
if err != nil {
322328
return errors.Wrapf(err, "failed to parse vertex digest %s", ve.Vertex.Digest)
@@ -328,6 +334,7 @@ workers0:
328334
out.Error.Name = name
329335
out.Error.Logs = logs
330336
}
337+
out.Error.Stack = []byte(fmt.Sprintf("%+v", stack.Formatter(retErr)))
331338
}
332339
}
333340

@@ -566,10 +573,9 @@ workers0:
566573
}
567574

568575
if out.Error != nil {
569-
for _, s := range errdefs.Sources(out.Error.statusErr) {
570-
s.Print(dockerCli.Out())
576+
if out.Error.Sources != nil {
577+
fmt.Fprint(dockerCli.Out(), string(out.Error.Sources))
571578
}
572-
fmt.Fprintln(dockerCli.Out())
573579
if len(out.Error.Logs) > 0 {
574580
fmt.Fprintln(dockerCli.Out(), "Logs:")
575581
fmt.Fprintf(dockerCli.Out(), "> => %s:\n", out.Error.Name)
@@ -578,10 +584,12 @@ workers0:
578584
}
579585
fmt.Fprintln(dockerCli.Out())
580586
}
581-
if debug.IsEnabled() {
582-
fmt.Fprintf(dockerCli.Out(), "\n%+v\n", stack.Formatter(out.Error.statusErr))
583-
} else if len(stack.Traces(out.Error.statusErr)) > 0 {
584-
fmt.Fprintf(dockerCli.Out(), "Enable --debug to see stack traces for error\n")
587+
if len(out.Error.Stack) > 0 {
588+
if debug.IsEnabled() {
589+
fmt.Fprintf(dockerCli.Out(), "\n%s\n", out.Error.Stack)
590+
} else {
591+
fmt.Fprintf(dockerCli.Out(), "Enable --debug to see stack traces for error\n")
592+
}
585593
}
586594
}
587595

0 commit comments

Comments
 (0)