Skip to content

Commit

Permalink
Add panic recovery handlers and improve error logging
Browse files Browse the repository at this point in the history
- Implemented panic recovery in the main function to log errors.
- Wrapped HTTP request handlers with panic recovery to return error responses.
- Enhanced input method switching with error handling and logging for failures.
- Added context support for panic recovery in the IME switch function.
  • Loading branch information
qianlifeng committed Dec 2, 2024
1 parent 1e81e77 commit 1af4b7f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Wox/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func main() {
panic(locationErr)
}

defer util.GoRecover(context.Background(), "main panic", func(err error) {
util.GetLogger().Error(context.Background(), fmt.Sprintf("main panic: %s", err.Error()))
})

ctx := util.NewTraceContext()
util.GetLogger().Info(ctx, "------------------------------")
util.GetLogger().Info(ctx, "Wox starting")
Expand Down
9 changes: 8 additions & 1 deletion Wox/ui/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,14 @@ func serveAndWait(ctx context.Context, port int) {
mux := http.NewServeMux()

for path, callback := range routers {
mux.HandleFunc(path, callback)
//add panic handler
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
defer util.GoRecover(ctx, "http request panic", func(err error) {
writeErrorResponse(w, err.Error())
})

callback(w, r)
})
}

mux.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
Expand Down
5 changes: 4 additions & 1 deletion Wox/ui/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,10 @@ func (m *Manager) PostOnShow(ctx context.Context) {
woxSetting := setting.GetSettingManager().GetWoxSetting(ctx)
if woxSetting.SwitchInputMethodABC {
util.GetLogger().Info(ctx, "switch input method to ABC")
ime.SwitchInputMethodABC()
switchErr := ime.SwitchInputMethodABC()
if switchErr != nil {
logger.Error(ctx, fmt.Sprintf("failed to switch input method to ABC: %s", switchErr.Error()))
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions Wox/util/ime/ime_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ void switchInputMethod(const char *inputMethodID);
*/
import "C"
import (
"context"
"errors"
"unsafe"
"wox/util"

"golang.design/x/hotkey/mainthread"
)
Expand All @@ -23,6 +25,10 @@ func SwitchInputMethodABC() error {
errorChan := make(chan error)

mainthread.Call(func() {
defer util.GoRecover(context.Background(), "switch input method panic", func(err error) {
errorChan <- err
})

inputMethod := C.GoString(C.getCurrentInputMethod())
if inputMethod == "" {
errorChan <- errors.New("failed to get current input method")
Expand Down

0 comments on commit 1af4b7f

Please sign in to comment.