FAQ
Reviewers: golang-dev_googlegroups.com,

Message:
Hello golang-dev@googlegroups.com (cc: golang-dev@googlegroups.com),

I'd like you to review this change to
https://code.google.com/p/go/


Description:
encoding/csv: add Error method to Writer

Fixed issue 3931

Please review this at https://codereview.appspot.com/6923049/

Affected files:
M src/pkg/encoding/csv/writer.go
M src/pkg/encoding/csv/writer_test.go


Index: src/pkg/encoding/csv/writer.go
===================================================================
--- a/src/pkg/encoding/csv/writer.go
+++ b/src/pkg/encoding/csv/writer.go
@@ -92,10 +92,17 @@
}

// 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 can be called after Flush for clients who want to be careful
+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: src/pkg/encoding/csv/writer_test.go
===================================================================
--- a/src/pkg/encoding/csv/writer_test.go
+++ b/src/pkg/encoding/csv/writer_test.go
@@ -6,6 +6,7 @@

import (
"bytes"
+ "errors"
"testing"
)

@@ -42,3 +43,30 @@
}
}
}
+
+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")
+ }
+}

Search Discussions

Discussion Posts

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 1 of 11 | next ›
Discussion Overview
groupgolang-dev @
categoriesgo
postedDec 11, '12 at 2:53p
activeDec 11, '12 at 7:37p
posts11
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase