From ffb49ef022e6011ced51ffeee0e9e7779be95018 Mon Sep 17 00:00:00 2001 From: Siarhiej Siemianczuk Date: Wed, 24 Jul 2024 06:37:31 +0300 Subject: [PATCH] cmds/core/head: improve error handling Signed-off-by: Siarhiej Siemianczuk --- cmds/core/head/head.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/cmds/core/head/head.go b/cmds/core/head/head.go index 4f3d11625a..ed939e5a66 100644 --- a/cmds/core/head/head.go +++ b/cmds/core/head/head.go @@ -6,6 +6,7 @@ package main import ( "bufio" + "errors" "flag" "fmt" "io" @@ -33,11 +34,12 @@ func run(stdout io.Writer, stderr io.Writer, bytes, count int, files ...string) } var newLineHeader bool + var errs error for _, file := range files { f, err := os.Open(file) if err != nil { - fmt.Fprintf(stderr, "head: %v", err) + errs = errors.Join(errs, fmt.Errorf("head: %w", err)) continue } if len(files) > 1 { @@ -53,7 +55,7 @@ func run(stdout io.Writer, stderr io.Writer, bytes, count int, files ...string) if err == io.ErrUnexpectedEOF { // ignore if user request more bytes than file has } else if err != nil { - fmt.Fprintf(stderr, "head: %v", err) + errs = errors.Join(errs, fmt.Errorf("head: %w", err)) continue } stdout.Write(buffer[:n]) @@ -70,6 +72,9 @@ func run(stdout io.Writer, stderr io.Writer, bytes, count int, files ...string) } } + if errs != nil { + fmt.Fprintf(stderr, "\n%v\n", errs) + } return nil }