Skip to content

Instantly share code, notes, and snippets.

@lucasrpb
Last active December 18, 2016 00:02
Show Gist options
  • Save lucasrpb/8f4dc8c0220aadbfc27e61b681a79acc to your computer and use it in GitHub Desktop.
Save lucasrpb/8f4dc8c0220aadbfc27e61b681a79acc to your computer and use it in GitHub Desktop.
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