2020-04-22 04:40:44 +00:00
|
|
|
package errdefs
|
|
|
|
|
2020-04-23 01:30:19 +00:00
|
|
|
import (
|
2020-04-29 20:37:05 +00:00
|
|
|
"fmt"
|
2020-04-28 04:25:41 +00:00
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
2020-04-27 23:35:20 +00:00
|
|
|
pb "github.com/moby/buildkit/solver/pb"
|
2020-04-23 01:30:19 +00:00
|
|
|
"github.com/moby/buildkit/util/grpcerrors"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
2020-04-22 04:40:44 +00:00
|
|
|
|
2020-05-01 18:39:45 +00:00
|
|
|
func WithSource(err error, src Source) error {
|
2020-04-22 04:40:44 +00:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-01 18:39:45 +00:00
|
|
|
return &ErrorSource{Source: src, error: err}
|
2020-04-22 04:40:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ErrorSource struct {
|
|
|
|
Source
|
|
|
|
error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ErrorSource) Unwrap() error {
|
|
|
|
return e.error
|
|
|
|
}
|
|
|
|
|
2020-04-23 01:30:19 +00:00
|
|
|
func (e *ErrorSource) ToProto() grpcerrors.TypedErrorProto {
|
|
|
|
return &e.Source
|
|
|
|
}
|
|
|
|
|
2020-04-22 04:40:44 +00:00
|
|
|
func Sources(err error) []*Source {
|
|
|
|
var out []*Source
|
|
|
|
var es *ErrorSource
|
|
|
|
if errors.As(err, &es) {
|
|
|
|
out = Sources(es.Unwrap())
|
|
|
|
out = append(out, &es.Source)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
2020-04-23 01:30:19 +00:00
|
|
|
|
|
|
|
func (s *Source) WrapError(err error) error {
|
|
|
|
return &ErrorSource{error: err, Source: *s}
|
|
|
|
}
|
2020-04-28 04:25:41 +00:00
|
|
|
|
|
|
|
func (s *Source) Print(w io.Writer) error {
|
2020-05-01 18:39:45 +00:00
|
|
|
si := s.Info
|
|
|
|
if si == nil {
|
2020-04-27 23:35:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-05-01 18:39:45 +00:00
|
|
|
lines := strings.Split(string(si.Data), "\n")
|
2020-04-28 04:25:41 +00:00
|
|
|
|
2020-05-01 18:39:45 +00:00
|
|
|
start, end, ok := getStartEndLine(s.Locations)
|
2020-04-28 04:25:41 +00:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if start > len(lines) || start < 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if end > len(lines) {
|
|
|
|
end = len(lines)
|
|
|
|
}
|
|
|
|
|
|
|
|
pad := 2
|
|
|
|
if end == start {
|
|
|
|
pad = 4
|
|
|
|
}
|
|
|
|
var p int
|
|
|
|
|
2020-04-29 20:37:05 +00:00
|
|
|
prepadStart := start
|
2020-04-28 04:25:41 +00:00
|
|
|
for {
|
|
|
|
if p >= pad {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if start > 1 {
|
|
|
|
start--
|
|
|
|
p++
|
|
|
|
}
|
|
|
|
if end != len(lines) {
|
|
|
|
end++
|
|
|
|
p++
|
|
|
|
}
|
|
|
|
p++
|
|
|
|
}
|
|
|
|
|
2020-05-01 18:39:45 +00:00
|
|
|
fmt.Fprintf(w, "%s:%d\n--------------------\n", si.Filename, prepadStart)
|
2020-04-28 04:25:41 +00:00
|
|
|
for i := start; i <= end; i++ {
|
|
|
|
pfx := " "
|
2020-05-01 18:39:45 +00:00
|
|
|
if containsLine(s.Locations, i) {
|
2020-04-28 04:25:41 +00:00
|
|
|
pfx = ">>>"
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, " %3d | %s %s\n", i, pfx, lines[i-1])
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "--------------------\n")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-27 23:35:20 +00:00
|
|
|
func containsLine(rr []*pb.Range, l int) bool {
|
2020-04-28 04:25:41 +00:00
|
|
|
for _, r := range rr {
|
2020-05-01 18:39:45 +00:00
|
|
|
e := r.End.Line
|
|
|
|
if e < r.Start.Line {
|
|
|
|
e = r.Start.Line
|
2020-04-28 04:25:41 +00:00
|
|
|
}
|
2020-05-01 18:39:45 +00:00
|
|
|
if r.Start.Line <= int32(l) && e >= int32(l) {
|
2020-04-28 04:25:41 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-04-27 23:35:20 +00:00
|
|
|
func getStartEndLine(rr []*pb.Range) (start int, end int, ok bool) {
|
2020-05-01 18:39:45 +00:00
|
|
|
first := true
|
2020-04-28 04:25:41 +00:00
|
|
|
for _, r := range rr {
|
2020-05-01 18:39:45 +00:00
|
|
|
e := r.End.Line
|
|
|
|
if e < r.Start.Line {
|
|
|
|
e = r.Start.Line
|
|
|
|
}
|
|
|
|
if first || int(r.Start.Line) < start {
|
|
|
|
start = int(r.Start.Line)
|
2020-04-28 04:25:41 +00:00
|
|
|
}
|
2020-05-01 18:39:45 +00:00
|
|
|
if int(e) > end {
|
|
|
|
end = int(e)
|
2020-04-28 04:25:41 +00:00
|
|
|
}
|
2020-05-01 18:39:45 +00:00
|
|
|
first = false
|
2020-04-28 04:25:41 +00:00
|
|
|
}
|
2020-05-01 18:39:45 +00:00
|
|
|
return start, end, !first
|
2020-04-28 04:25:41 +00:00
|
|
|
}
|