From cfc87e64f03e55bf8894dc1efdb17b090dc31698 Mon Sep 17 00:00:00 2001 From: Mate Herber Date: Fri, 18 Mar 2022 17:36:52 +0100 Subject: [PATCH] Improve exit code error checking utilizing go error unwrap functionality --- errorutil/errorutil.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/errorutil/errorutil.go b/errorutil/errorutil.go index dd5f8d2..d7d8955 100644 --- a/errorutil/errorutil.go +++ b/errorutil/errorutil.go @@ -2,12 +2,14 @@ package errorutil import ( + "errors" "os/exec" "regexp" ) func exitCode(err error) int { - if exitError, ok := err.(*exec.ExitError); ok { + var exitError *exec.ExitError + if errors.As(err, &exitError) { return exitError.ProcessState.ExitCode() } return -1 @@ -22,7 +24,7 @@ func IsExitStatusError(err error) bool { func IsExitStatusErrorStr(errString string) bool { // https://golang.org/src/os/exec_posix.go?s=2421:2459#L87 // example exit status error string: exit status 1 - var rex = regexp.MustCompile(`^exit status [0-9]{1,3}$`) + var rex = regexp.MustCompile(`^exit status \d{1,3}$`) return rex.MatchString(errString) }