diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2017-12-01 00:58:53 -0500 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2017-12-01 00:58:53 -0500 |
commit | 4381fb12a5fe7ab3d32b73c355f32744abcd7afc (patch) | |
tree | 1ca9a59e0a61685666a338e47d0cb905ed55821b | |
parent | d14b88c9b4d03674a1a8ba631fe1330ddd457d56 (diff) |
better error tracking
-rw-r--r-- | errors.go | 52 | ||||
-rw-r--r-- | filter.go | 10 | ||||
-rw-r--r-- | git.go | 34 |
3 files changed, 80 insertions, 16 deletions
diff --git a/errors.go b/errors.go new file mode 100644 index 0000000..681ef4f --- /dev/null +++ b/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 + } +} @@ -2,7 +2,6 @@ package main import ( "bytes" - "fmt" "io" "io/ioutil" "os" @@ -11,6 +10,7 @@ import ( "strings" "git.lukeshu.com/go/libfastimport" + "github.com/pkg/errors" ) type fullcommit struct { @@ -86,7 +86,7 @@ func (f *Filter) pkgbuild2srcinfo(pkgbuildId string) (string, error) { return "", err } if sha1 != pkgbuildId { - return "", fmt.Errorf("PKGBUILD sha1 mismatch: %q != %q", pkgbuildId, sha1) + return "", errors.Errorf("PKGBUILD sha1 mismatch: %q != %q", pkgbuildId, sha1) } // Write the PKGBUILD to a temporary file to pass to makepkg file, err := os.OpenFile(f.infofile("tmp/PKGBUILD"), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) @@ -103,7 +103,7 @@ func (f *Filter) pkgbuild2srcinfo(pkgbuildId string) (string, error) { cmd.Dir = f.infofile("tmp") srcinfoBody, err := cmd.Output() if err != nil { - return "", err + return "", &ProcessError{ExitError: err.(*exec.ExitError), Cmd: "makepkg --printsrcinfo"} } // Write the .SRCINFO back in to git mark := f.newmark() @@ -164,7 +164,7 @@ func (f *Filter) Run() error { } cmd, err := f.frontend.ReadCmd() if cmd != nil || err != io.EOF { - return fmt.Errorf("git fast-export kept going after 'done': cmd=%v err=%v", cmd, err) + return errors.Errorf("git fast-export kept going after 'done': cmd=%v err=%v", cmd, err) } return nil case libfastimport.CmdReset: @@ -263,7 +263,7 @@ func (f *Filter) Run() error { branch_ref := f.toPfx + "/" + branchname from_dataref, from_dataref_ok := f.refs[branch_ref] if !from_dataref_ok { - return fmt.Errorf("cannot delete file %q from branch %q, because that branch doesn't exist!", cmdt.Path, branchname) + return errors.Errorf("cannot delete file %q from branch %q, because that branch doesn't exist!", cmdt.Path, branchname) } file_mode, _, _, err := f.backend.Ls(libfastimport.CmdLs{DataRef: from_dataref, Path: cmdt.Path}) if err != nil { @@ -2,12 +2,12 @@ package main import ( "bytes" - "fmt" "os" "os/exec" "strings" "git.lukeshu.com/go/libfastimport" + "github.com/pkg/errors" ) func gitFastExport(args ...string) (*libfastimport.Frontend, error) { @@ -27,13 +27,19 @@ func gitFastExport(args ...string) (*libfastimport.Frontend, error) { } frontend := libfastimport.NewFrontend(stdout, nil, func(err error) error { - if _err := cmd.Wait(); _err != nil { - if ee, ok := _err.(*exec.ExitError); ok { + errs := ErrorCollector{} + + errs.Add(err) + + if err := cmd.Wait(); err != nil { + if ee, ok := err.(*exec.ExitError); ok { ee.Stderr = stderr.Bytes() + err = &ProcessError{ExitError: ee, Cmd: "git fast-export"} } - err = _err + errs.Add(err) } - return err + + return errs.Err() }) return frontend, nil @@ -66,15 +72,21 @@ func gitFastImport(args ...string) (*libfastimport.Backend, error) { pipw.Close() backend := libfastimport.NewBackend(stdin, pipr, func(err error) error { - pipr.Close() + errs := ErrorCollector{} + + errs.Add(err) + stdin.Close() - if _err := cmd.Wait(); _err != nil { - if ee, ok := _err.(*exec.ExitError); ok { + pipr.Close() + if err = cmd.Wait(); err != nil { + if ee, ok := err.(*exec.ExitError); ok { ee.Stderr = stderr.Bytes() + err = &ProcessError{ExitError: ee, Cmd: "git fast-import"} } - err = _err + errs.Add(err) } - return err + + return errs.Err() }) return backend, nil @@ -95,7 +107,7 @@ func gitRefs() (map[string]string, error) { sp := strings.IndexByte(line, ' ') ht := strings.IndexByte(line, '\t') if sp < 0 || ht < sp { - return nil, fmt.Errorf("malformed git for-each-ref line: %q", line) + return nil, errors.Errorf("malformed git for-each-ref line: %q", line) } refTarget := line[:sp] refType := line[sp+1 : ht] |