Skip to content

Commit 00bbf18

Browse files
authored
Merge pull request #434 from cursor-home/master
fix: Improve panic recovery mechanism in main function
2 parents fc5ea35 + ec954b3 commit 00bbf18

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

cmd/cursor-id-modifier/main.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ import (
1111
"runtime/debug"
1212
"strings"
1313

14+
"github.com/sirupsen/logrus"
15+
1416
"github.com/yuaotian/go-cursor-help/internal/config"
1517
"github.com/yuaotian/go-cursor-help/internal/lang"
1618
"github.com/yuaotian/go-cursor-help/internal/process"
1719
"github.com/yuaotian/go-cursor-help/internal/ui"
1820
"github.com/yuaotian/go-cursor-help/pkg/idgen"
19-
20-
"github.com/sirupsen/logrus"
2121
)
2222

2323
// Global variables
@@ -29,7 +29,15 @@ var (
2929
)
3030

3131
func main() {
32-
setupErrorRecovery()
32+
// Place defer at the beginning of main to ensure it can catch panics from all subsequent function calls
33+
defer func() {
34+
if r := recover(); r != nil {
35+
log.Errorf("Panic recovered: %v\n", r)
36+
debug.PrintStack()
37+
waitExit()
38+
}
39+
}()
40+
3341
handleFlags()
3442
setupLogger()
3543

@@ -73,16 +81,6 @@ func main() {
7381
}
7482
}
7583

76-
func setupErrorRecovery() {
77-
defer func() {
78-
if r := recover(); r != nil {
79-
log.Errorf("Panic recovered: %v\n", r)
80-
debug.PrintStack()
81-
waitExit()
82-
}
83-
}()
84-
}
85-
8684
func handleFlags() {
8785
flag.Parse()
8886
if *showVersion {

0 commit comments

Comments
 (0)