forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
49 lines (42 loc) · 1.32 KB
/
error.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
package fmt
import (
"strings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
const (
Indent = " "
Bullet = "- "
)
func MultilineError(err error) string {
return prefixingMultilineError(err, "", "")
}
func prefixingMultilineError(err error, prefix string, bullet string) string {
currPrefix := prefix + bullet
prefix = prefix + strings.Repeat(" ", len(bullet))
switch specificErr := err.(type) {
case bosherr.ComplexError:
return currPrefix + specificErr.Err.Error() + ":\n" + prefixingMultilineError(specificErr.Cause, prefix+Indent, "")
case bosherr.MultiError:
lines := make([]string, len(specificErr.Errors), len(specificErr.Errors))
for i, sibling := range specificErr.Errors {
lines[i] = prefixingMultilineError(sibling, prefix, Bullet)
}
return strings.Join(lines, "\n")
case boshsys.ExecError:
lines := []string{
"Error Executing Command:",
prefixEachLine(specificErr.Command, Indent),
"StdOut:",
prefixEachLine(specificErr.StdOut, Indent),
"StdErr:",
prefixEachLine(specificErr.StdErr, Indent),
}
return prefixEachLine(strings.Join(lines, "\n"), prefix)
default:
return currPrefix + specificErr.Error()
}
}
func prefixEachLine(str string, prefix string) string {
return prefix + strings.Replace(str, "\n", "\n"+prefix, -1)
}