On Wednesday, February 7, 2018 at 8:54:12 AM UTC-5, Jakob Borg wrote:
>
> On 7 Feb 2018, at 14:45, Justin Azoff <[email protected] <javascript:>>
> wrote:
>
>
> Is there some way to inspect that error to figure out that it was related
> to reading from 'gr' and not writing to /dev/null?
>
>
> Two options that come to mind are
>
> - handling the copy yourself so you get separate Read() and Write() calls
> and can handle their returns accordingly
>
Yes.. I see one easy way to do this.. right now the backend interface I
came up with uses
Filter(reader io.Reader, query string, writer io.Writer) error
But if I changed that to
Filter(reader io.Reader, query string) (io.Reader, error)
I could do all the copying in one place, and really easily differentiate
between a read error and a write error. I think this is also the more
elegant interface. The 'backends' are really responsible for reading from
the log files and shouldn't be concerned with output.
- wrapping the writer in a type that wraps the Write() call and retains
> any error returned. You can then ask the writer if it encountered an error
> after getting an error return from io.Copy.
>
So simple! This would probably work for the Reader side too.
> I’m sure there are other ways.
>
Indeed! I think my mistake was the Filter interface.. having it be
responsible for both filtering and writing is probably what caused this
issue. If Filter just returns a filtered Reader I'm left with an io.Reader
and an io.Writer that I can deal with separately. I think the ultimate
solution will be a combination of both.. Simplify the interface and wrap
the Writer with a struct that remembers the last error. Then I think i
would look something like
r, _ = Filter(r, query)
w = LastErrorWrapper(w)
_, err = io.Copy(w, r)
if err != nil and w.LastError != nil { # error writing to output, abort.
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.