Last active
December 18, 2016 00:02
-
-
Save lucasrpb/8f4dc8c0220aadbfc27e61b681a79acc to your computer and use it in GitHub Desktop.
This file contains hidden or 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 main | |
import ( | |
"fmt" | |
"runtime" | |
) | |
func catchPanic(err *error, functionName string) { | |
// recover gets the last panic | |
if r := recover(); r != nil { | |
// Capture the stack trace | |
buf := make([]byte, 10000) | |
runtime.Stack(buf, false) | |
fmt.Printf("%s : Stack Trace : %s", functionName, string(buf)) | |
if err != nil { | |
*err = fmt.Errorf("%v", r) | |
} | |
} | |
} | |
func test(){ | |
/** | |
* Defer is a special function that executes after the current function executes! | |
* Deferers are executed in a LAST-IN FIRST-OUT FASHION | |
* (stacks all panic calls) | |
*/ | |
var err *error | |
defer catchPanic(err, "main") | |
panic("SOMETHING WENT TERRIBLY WRONG!!!") | |
} | |
func main(){ | |
/*var err error | |
defer catchPanic(&err, "main")*/ | |
test() | |
fmt.Println("\n\nthe code continues :P\n\n") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment