Skip to content

Instantly share code, notes, and snippets.

@Lemorz56
Created October 26, 2022 20:31
Show Gist options
  • Save Lemorz56/25f56a1f11fbab66a3bf59af0884ace9 to your computer and use it in GitHub Desktop.
Save Lemorz56/25f56a1f11fbab66a3bf59af0884ace9 to your computer and use it in GitHub Desktop.
package main
import (
"errors"
"fmt"
"os"
"path"
"runtime/debug"
"time"
)
const appVersion = "0.1.3"
func main() {
defer panicRecover()
Run()
}
func Run() {
fmt.Println("Working...")
time.Sleep(5 * time.Second)
err := errors.New("im an error")
panic(err)
}
func panicRecover() {
recovered := recover()
if recovered == nil {
return
}
exe, err := os.Executable()
if err != nil {
return
}
dir := path.Dir(exe)
appError, ok := recovered.(error)
if ok {
timestamp := time.Now().String()
content := fmt.Sprintf("%s\nVersion: %s\n\nError: %s\n%s", timestamp, appVersion, appError.Error(), string(debug.Stack()))
_ = os.WriteFile(path.Join(dir, "error-log.txt"), []byte(content), 0777)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment