github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/internal/errs/exitcode.go (about) 1 package errs 2 3 import "errors" 4 5 type ExitCodeable interface { 6 ExitCode() int 7 } 8 9 type ExitCode struct { 10 code int 11 wrappedErr error 12 } 13 14 func WrapExitCode(err error, code int) error { 15 return &ExitCode{code, err} 16 } 17 18 func (e *ExitCode) Error() string { 19 return "ExitCode" 20 } 21 22 func (e *ExitCode) Unwrap() error { 23 return e.wrappedErr 24 } 25 26 func (e *ExitCode) ExitCode() int { 27 return e.code 28 } 29 30 // ParseExitCode checks if the given error is a failure of type ExitCodeable and 31 // returns the ExitCode of the process that failed with this error 32 func ParseExitCode(err error) int { 33 if err == nil { 34 return 0 35 } 36 37 var eerr ExitCodeable 38 if errors.As(err, &eerr) { 39 return eerr.ExitCode() 40 } 41 42 return 1 43 }