From d9498148b860252c6f80eb6cd5acbd9fe07b2b8b Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Fri, 17 Nov 2017 16:29:37 -0500 Subject: error callbacks --- backend.go | 54 ++++++++++++++++++++++++++++++++++++++---------------- frontend.go | 5 ++++- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/backend.go b/backend.go index 3bf07b5..b91ee88 100644 --- a/backend.go +++ b/backend.go @@ -13,58 +13,80 @@ type Backend struct { w *bufio.Writer fiw *textproto.FIWriter cbr *textproto.CatBlobReader + + onErr func(error) error } -func NewBackend(fastImport io.Writer, catBlob io.Reader) *Backend { - ret := Backend{} +func NewBackend(fastImport io.Writer, catBlob io.Reader, onErr func(error) error) *Backend { + ret := &Backend{} ret.w = bufio.NewWriter(fastImport) ret.fiw = textproto.NewFIWriter(ret.w) if catBlob != nil { ret.cbr = textproto.NewCatBlobReader(catBlob) } - return &ret + ret.onErr = onErr + return ret } func (b *Backend) Do(cmd Cmd) error { err := cmd.fiWriteCmd(b.fiw) if err != nil { - return err + return b.onErr(err) + } + err = b.w.Flush() + if err != nil { + return b.onErr(err) } - return b.w.Flush() + return nil } -func (b *Backend) GetMark(cmd CmdGetMark) (string, error) { - err := b.Do(cmd) +func (b *Backend) GetMark(cmd CmdGetMark) (sha1 string, err error) { + err = b.Do(cmd) if err != nil { - return "", err + return } line, err := b.cbr.ReadLine() if err != nil { - return "", err + err = b.onErr(err) + return + } + sha1, err = cbpGetMark(line) + if err != nil { + err = b.onErr(err) } - return cbpGetMark(line) + return } func (b *Backend) CatBlob(cmd CmdCatBlob) (sha1 string, data string, err error) { err = b.Do(cmd) if err != nil { - return "", "", err + return } line, err := b.cbr.ReadLine() if err != nil { - return "", "", err + err = b.onErr(err) + return + } + sha1, data, err = cbpCatBlob(line) + if err != nil { + err = b.onErr(err) } - return cbpCatBlob(line) + return } func (b *Backend) Ls(cmd CmdLs) (mode textproto.Mode, dataref string, path textproto.Path, err error) { err = b.Do(cmd) if err != nil { - return 0, "", "", err + return } line, err := b.cbr.ReadLine() if err != nil { - return 0, "", "", err + err = b.onErr(err) + return + } + mode, dataref, path, err = cbpLs(line) + if err != nil { + err = b.onErr(err) } - return cbpLs(line) + return } diff --git a/frontend.go b/frontend.go index 0b10f65..2150a35 100644 --- a/frontend.go +++ b/frontend.go @@ -38,7 +38,7 @@ type Frontend struct { err error } -func NewFrontend(fastImport io.Reader, catBlob io.Writer) *Frontend { +func NewFrontend(fastImport io.Reader, catBlob io.Writer, onErr func(error) error) *Frontend { ret := &Frontend{} ret.fir = textproto.NewFIReader(fastImport) if catBlob == nil { @@ -49,6 +49,9 @@ func NewFrontend(fastImport io.Reader, catBlob io.Writer) *Frontend { ret.cmd = make(chan Cmd) go func() { ret.err = ret.parse() + if onErr != nil { + ret.err = onErr(ret.err) + } close(ret.cmd) }() return ret -- cgit v1.2.3-54-g00ecf