diff options
| author | Aleksandr Nogikh <nogikh@google.com> | 2025-09-26 15:24:20 +0200 |
|---|---|---|
| committer | Aleksandr Nogikh <nogikh@google.com> | 2025-10-01 14:41:08 +0000 |
| commit | 12039a0e63377e86fa1b5adc3d327d6e07f23a32 (patch) | |
| tree | 0d622daed4041159362850994ab81d8ce351cbff /pkg/osutil | |
| parent | db09e5b2322f6dbd902a69405c2142617c6b8715 (diff) | |
pkg/osutil: make VerboseError nest other errors
After this change it fits more naturally into the Go's error
functionality.
Diffstat (limited to 'pkg/osutil')
| -rw-r--r-- | pkg/osutil/osutil.go | 24 |
1 files changed, 7 insertions, 17 deletions
diff --git a/pkg/osutil/osutil.go b/pkg/osutil/osutil.go index d16a76cb7..1379e8c23 100644 --- a/pkg/osutil/osutil.go +++ b/pkg/osutil/osutil.go @@ -65,9 +65,9 @@ func Run(timeout time.Duration, cmd *exec.Cmd) ([]byte, error) { err := cmd.Wait() close(done) if err != nil { - text := fmt.Sprintf("failed to run %q: %v", cmd.Args, err) + retErr := fmt.Errorf("failed to run %q: %w", cmd.Args, err) if <-timedout { - text = fmt.Sprintf("timedout after %v %q", timeout, cmd.Args) + retErr = fmt.Errorf("timedout after %v %q", timeout, cmd.Args) } exitCode := cmd.ProcessState.ExitCode() var exitErr *exec.ExitError @@ -77,7 +77,7 @@ func Run(timeout time.Duration, cmd *exec.Cmd) ([]byte, error) { } } return output.Bytes(), &VerboseError{ - Title: text, + Err: retErr, Output: output.Bytes(), ExitCode: exitCode, } @@ -111,27 +111,17 @@ func GraciousCommand(bin string, args ...string) *exec.Cmd { } type VerboseError struct { - Title string + Err error Output []byte ExitCode int } func (err *VerboseError) Error() string { - if len(err.Output) == 0 { - return err.Title - } - return fmt.Sprintf("%v\n%s", err.Title, err.Output) + return err.Err.Error() } -func PrependContext(ctx string, err error) error { - var verboseError *VerboseError - switch { - case errors.As(err, &verboseError): - verboseError.Title = fmt.Sprintf("%v: %v", ctx, verboseError.Title) - return verboseError - default: - return fmt.Errorf("%v: %w", ctx, err) - } +func (err *VerboseError) Unwrap() error { + return err.Err } func IsDir(name string) bool { |
