@@ -214,11 +214,14 @@ func (p *Pipe) Basename() *Pipe {
214214
215215// Bytes returns the contents of the pipe as a []byte, or an error.
216216func (p * Pipe ) Bytes () ([]byte , error ) {
217- res , err := io .ReadAll (p )
217+ if p .Error () != nil {
218+ return nil , p .Error ()
219+ }
220+ data , err := io .ReadAll (p )
218221 if err != nil {
219222 p .SetError (err )
220223 }
221- return res , err
224+ return data , nil
222225}
223226
224227// Close closes the pipe's associated reader. This is a no-op if the reader is
@@ -405,9 +408,6 @@ func (p *Pipe) Exec(cmdLine string) *Pipe {
405408//
406409// ListFiles("*").ExecForEach("touch {{.}}").Wait()
407410func (p * Pipe ) ExecForEach (cmdLine string ) * Pipe {
408- if p .Error () != nil {
409- return p
410- }
411411 tpl , err := template .New ("" ).Parse (cmdLine )
412412 if err != nil {
413413 return p .WithError (err )
@@ -467,6 +467,9 @@ func (p *Pipe) ExitStatus() int {
467467// been fully read. Use [Pipe.Wait] to wait for all concurrent filters to
468468// complete.
469469func (p * Pipe ) Filter (filter func (io.Reader , io.Writer ) error ) * Pipe {
470+ if p .Error () != nil {
471+ return p
472+ }
470473 pr , pw := io .Pipe ()
471474 q := NewPipe ().WithReader (pr )
472475 go func () {
@@ -504,6 +507,9 @@ func (p *Pipe) FilterScan(filter func(string, io.Writer)) *Pipe {
504507// lines if there are less than n. If n is zero or negative, there is no output
505508// at all.
506509func (p * Pipe ) First (n int ) * Pipe {
510+ if p .Error () != nil {
511+ return p
512+ }
507513 if n <= 0 {
508514 return NewPipe ()
509515 }
@@ -639,6 +645,9 @@ func (p *Pipe) JQ(query string) *Pipe {
639645// if there are less than n. If n is zero or negative, there is no output at
640646// all.
641647func (p * Pipe ) Last (n int ) * Pipe {
648+ if p .Error () != nil {
649+ return p
650+ }
642651 if n <= 0 {
643652 return NewPipe ()
644653 }
@@ -691,6 +700,9 @@ func (p *Pipe) Post(URL string) *Pipe {
691700// bytes read and any error encountered. At end of file, or on a nil pipe, Read
692701// returns 0, [io.EOF].
693702func (p * Pipe ) Read (b []byte ) (int , error ) {
703+ if p .Error () != nil {
704+ return 0 , p .Error ()
705+ }
694706 return p .Reader .Read (b )
695707}
696708
@@ -742,6 +754,9 @@ func (p *Pipe) SetError(err error) {
742754// SHA256Sum returns the hex-encoded SHA-256 hash of the entire contents of the
743755// pipe, or an error.
744756func (p * Pipe ) SHA256Sum () (string , error ) {
757+ if p .Error () != nil {
758+ return "" , p .Error ()
759+ }
745760 hasher := sha256 .New ()
746761 _ , err := io .Copy (hasher , p )
747762 if err != nil {
@@ -788,6 +803,9 @@ func (p *Pipe) Slice() ([]string, error) {
788803// [Pipe.WithStdout]), or to [os.Stdout] otherwise, and returns the number of
789804// bytes successfully written, together with any error.
790805func (p * Pipe ) Stdout () (int , error ) {
806+ if p .Error () != nil {
807+ return 0 , p .Error ()
808+ }
791809 n64 , err := io .Copy (p .stdout , p )
792810 if err != nil {
793811 return 0 , err
@@ -857,6 +875,9 @@ func (p *Pipe) WriteFile(path string) (int64, error) {
857875}
858876
859877func (p * Pipe ) writeOrAppendFile (path string , mode int ) (int64 , error ) {
878+ if p .Error () != nil {
879+ return 0 , p .Error ()
880+ }
860881 out , err := os .OpenFile (path , mode , 0666 )
861882 if err != nil {
862883 p .SetError (err )
0 commit comments