forked from cloudfoundry-attic/bosh-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
43 lines (37 loc) · 1.18 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
package fmt
import (
"strings"
bosherr "github.com/cloudfoundry/bosh-agent/errors"
boshsys "github.com/cloudfoundry/bosh-agent/system"
)
var Indent = " "
func MultilineError(err error) string {
return prefixingMultilineError(err, "")
}
func prefixingMultilineError(err error, prefix string) string {
switch specificErr := err.(type) {
case bosherr.ComplexError:
return prefix + 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)
}
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 prefix + specificErr.Error()
}
}
func prefixEachLine(str string, prefix string) string {
return prefix + strings.Replace(str, "\n", "\n"+prefix, -1)
}