Add proper error handling semantics to FileStream.

This commit is contained in:
David Sisson 2017-04-27 21:40:44 -07:00
parent 8c05696ed4
commit 32a8874a29
1 changed files with 11 additions and 5 deletions

View File

@ -19,13 +19,19 @@ type FileStream struct {
filename string
}
func NewFileStream(fileName string) *FileStream {
func NewFileStream(fileName string) (*FileStream, error) {
buf := bytes.NewBuffer(nil)
f, _ := os.Open(fileName) // Error handling elided for brevity.
io.Copy(buf, f) // Error handling elided for brevity.
f.Close()
f, err := os.Open(fileName)
if err != nil {
return nil, err
}
defer f.Close()
err = io.Copy(buf, f)
if err != nil {
return nil, er
}
fs := new(FileStream)
@ -34,7 +40,7 @@ func NewFileStream(fileName string) *FileStream {
fs.InputStream = NewInputStream(s)
return fs
return fs, nil
}