WithExitCodeIfNone can attach an exit code to the given error, if it doesn't have one already. It won't do anything if the error already had an exit code attached. Similarly, if there is no error (i.e. the given error is nil), it also won't do anything.
(err error, exitCode exitcodes.ExitCode)
| 21 | // attached. Similarly, if there is no error (i.e. the given error is nil), it |
| 22 | // also won't do anything. |
| 23 | func WithExitCodeIfNone(err error, exitCode exitcodes.ExitCode) error { |
| 24 | if err == nil { |
| 25 | // No error, do nothing |
| 26 | return nil |
| 27 | } |
| 28 | var ecerr HasExitCode |
| 29 | if errors.As(err, &ecerr) { |
| 30 | // The given error already has an exit code, do nothing |
| 31 | return err |
| 32 | } |
| 33 | return withExitCodeError{err, exitCode} |
| 34 | } |
| 35 | |
| 36 | type withExitCodeError struct { |
| 37 | error |
no outgoing calls
searching dependent graphs…