diff options
Diffstat (limited to 'fi-svntogit-to-aur/errors.go')
-rw-r--r-- | fi-svntogit-to-aur/errors.go | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/fi-svntogit-to-aur/errors.go b/fi-svntogit-to-aur/errors.go new file mode 100644 index 0000000..681ef4f --- /dev/null +++ b/fi-svntogit-to-aur/errors.go @@ -0,0 +1,52 @@ +package main + +import ( + "os/exec" + "strings" +) + +type ProcessError struct { + *exec.ExitError + Cmd string +} + +func (e *ProcessError) Error() string { + lines := strings.Split(string(e.ExitError.Stderr), "\n") + if lines[len(lines)-1] == "" { + lines = lines[:len(lines)-1] + } + ret := e.Cmd + ": " + e.ExitError.Error() + if len(lines) > 0 { + ret += ":\n > " + strings.Join(lines, "\n > ") + } + return ret +} + +type ErrorCollector struct { + errs []error +} + +func (ec ErrorCollector) Error() string { + lines := make([]string, len(ec.errs)) + for i := range ec.errs { + lines[i] = ec.errs[i].Error() + } + return strings.Join(lines, "\n") +} + +func (ec *ErrorCollector) Add(err error) { + if err != nil { + ec.errs = append(ec.errs, err) + } +} + +func (ec ErrorCollector) Err() error { + switch len(ec.errs) { + case 0: + return nil + case 1: + return ec.errs[0] + default: + return ec + } +} |