Skip to content

Commit

Permalink
Move FatalOnErr to exitcode.go
Browse files Browse the repository at this point in the history
  • Loading branch information
roeldev committed Mar 1, 2023
1 parent dc618d7 commit af2f02b
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions exitcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package errors

import (
"fmt"
"os"
)

// ExitCoder interfaces provide access to an exit code.
Expand Down Expand Up @@ -58,6 +59,15 @@ func GetExitCodeOr(err error, or int) int {
return or
}

// FatalOnErr prints the error to stderr and exits the program with an exit
// code that is not 0. When err is an ExitCoder its exit code is used.
func FatalOnErr(err error) {
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "\nFatal error: %+v\n", err)
os.Exit(GetExitCodeOr(err, 1))
}
}

type exitCodeError struct {
*embedError
exitCode int
Expand Down

0 comments on commit af2f02b

Please sign in to comment.