-
Notifications
You must be signed in to change notification settings - Fork 199
/
errors.go
64 lines (50 loc) · 1.3 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package config
import (
"bytes"
"fmt"
"gopkg.in/yaml.v2"
"github.com/werf/werf/v2/pkg/util"
)
type configError struct {
s string
}
func (e *configError) Error() string {
return e.s
}
func newConfigError(message string) error {
return &configError{message}
}
func newDetailedConfigError(message string, configSection interface{}, configDoc *doc) error {
var errorString string
if configSection != nil {
errorString = fmt.Sprintf("%s\n\n%s\n%s", message, dumpConfigSection(configSection), dumpConfigDoc(configDoc))
} else {
errorString = fmt.Sprintf("%s\n\n%s", message, dumpConfigDoc(configDoc))
}
return newConfigError(errorString)
}
func getLines(data []byte) [][]byte {
contentLines := bytes.Split(data, []byte("\n"))
if string(contentLines[len(contentLines)-1]) == "" {
contentLines = contentLines[0 : len(contentLines)-1]
}
return contentLines
}
func dumpConfigSection(config interface{}) string {
d, err := yaml.Marshal(config)
if err != nil {
return ""
}
contentLines := getLines(d)
res := ""
for _, lineBytes := range contentLines {
res += fmt.Sprintf(" %s\n", string(lineBytes))
}
return res
}
func dumpConfigDoc(doc *doc) string {
res := fmt.Sprintf("%s\n\n", doc.RenderFilePath)
res += util.NumerateLines(string(doc.Content), doc.Line+1)
res += "\n"
return res
}