Created
October 26, 2022 20:31
-
-
Save Lemorz56/25f56a1f11fbab66a3bf59af0884ace9 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 ( | |
"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