Created
March 9, 2012 07:30
-
-
Save bemasher/2005471 to your computer and use it in GitHub Desktop.
An error handling package for Golang.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package error | |
import ( | |
"os" | |
"fmt" | |
"runtime" | |
"path/filepath" | |
) | |
// Handle an error for the calling function | |
func Handle(msg string, err error) { | |
HandleDepth(msg, err, 2) | |
} | |
// Handle an error for any function at <depth> from the top of the call stack | |
func HandleDepth(msg string, err error, depth int) { | |
// If the error is non-nil | |
if err != nil { | |
// Find out who called it and where from, skip the top <depth> calls | |
pc, file, line, ok := runtime.Caller(depth) | |
// Parse out the filename and calling function | |
filename := filepath.Base(file) | |
callingFunc := runtime.FuncForPC(pc) | |
callingFuncName := callingFunc.Name() | |
// If we could retrieve the information then print a message and exit with an error | |
if ok { | |
fmt.Printf("%s:%s:%d: %s %s\n", filename, callingFuncName, line, msg, err) | |
os.Exit(1) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment