]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/csv: add Error method to Writer
authorRyan Slade <ryanslade@gmail.com>
Tue, 11 Dec 2012 18:29:13 +0000 (13:29 -0500)
committerRuss Cox <rsc@golang.org>
Tue, 11 Dec 2012 18:29:13 +0000 (13:29 -0500)
Fixed issue 3931

R=bradfitz, rsc
CC=golang-dev
https://golang.org/cl/6923049

src/pkg/encoding/csv/writer.go
src/pkg/encoding/csv/writer_test.go

index 17e485083e9d829b5cb815a3cbe43c11f2ac99c5..221542456ba41bd1c112462311501254fd587a9a 100644 (file)
@@ -92,10 +92,17 @@ func (w *Writer) Write(record []string) (err error) {
 }
 
 // Flush writes any buffered data to the underlying io.Writer.
+// To check if an error occured during the Flush, call Error.
 func (w *Writer) Flush() {
        w.w.Flush()
 }
 
+// Error reports any error that has occurred during a previous Write or Flush.
+func (w *Writer) Error() error {
+       _, err := w.w.Write(nil)
+       return err
+}
+
 // WriteAll writes multiple CSV records to w using Write and then calls Flush.
 func (w *Writer) WriteAll(records [][]string) (err error) {
        for _, record := range records {
index 578959007fd37f58fb745b559debb2dbd88c9c7f..03ca6b093c03b6ebc169330b88ca24d1570ea52c 100644 (file)
@@ -6,6 +6,7 @@ package csv
 
 import (
        "bytes"
+       "errors"
        "testing"
 )
 
@@ -42,3 +43,30 @@ func TestWrite(t *testing.T) {
                }
        }
 }
+
+type errorWriter struct{}
+
+func (e errorWriter) Write(b []byte) (int, error) {
+       return 0, errors.New("Test")
+}
+
+func TestError(t *testing.T) {
+       b := &bytes.Buffer{}
+       f := NewWriter(b)
+       f.Write([]string{"abc"})
+       f.Flush()
+       err := f.Error()
+
+       if err != nil {
+               t.Errorf("Unexpected error: %s\n", err)
+       }
+
+       f = NewWriter(errorWriter{})
+       f.Write([]string{"abc"})
+       f.Flush()
+       err = f.Error()
+
+       if err == nil {
+               t.Error("Error should not be nil")
+       }
+}