Skip to content

Commit

Permalink
added DieIf/PrintfIf to help with errors
Browse files Browse the repository at this point in the history
  • Loading branch information
OneOfOne committed Sep 15, 2014
1 parent 046c5da commit 9caaaf3
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions errors/logandstuff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package errors

import (
"fmt"
"os"
)

// DieIf prints msg + err to stderr then exits the program with status code 1
// it's like a panic, without the stacktrace.
// use %v in message to print the error, default is "error: %v"
func DieIf(err error, msg string) {
if err != nil {
if len(msg) == 0 {
msg = "error: %v"
}
fmt.Fprintf(os.Stderr, msg+"\n", err)
os.Exit(1)
}
}

// PrintfIf calls fn (log.Printf or fmt.Printf for example) with msg and err
// if err != nil, it will return true and call fn(msg, err)
// example:
// if PrintfIf(err, "error: %v\n", log.Printf) {
// return
// }
func PrintfIf(err error, msg string, fn func(format string, args ...interface{})) (b bool) {
if b = err != nil; b {
if len(msg) == 0 {
msg = "error: %v"
}
fn(msg, err)
}
return
}

0 comments on commit 9caaaf3

Please sign in to comment.