diff --git a/app.go b/app.go index e589f58..50ce81b 100644 --- a/app.go +++ b/app.go @@ -41,6 +41,10 @@ func NewApp() *App { // startup is called at application startup func (a *App) startup(ctx context.Context) { + defer PanicHandler() + runtime.EventsOn(ctx, "frontendError", func(optionalData ...interface{}) { + logger.SugaredLogger.Errorf("Frontend error: %v\n", optionalData) + }) logger.SugaredLogger.Infof("Version:%s", Version) // Perform your setup here a.ctx = ctx @@ -86,6 +90,8 @@ func checkUpdate(a *App) { // domReady is called after front-end resources have been loaded func (a *App) domReady(ctx context.Context) { + defer PanicHandler() + // Add your action here //定时更新数据 go func() { @@ -321,6 +327,7 @@ func addStockFollowData(follow data.FollowedStock, stockData *data.StockInfo) { // either by clicking the window close button or calling runtime.Quit. // Returning true will cause the application to continue, false will continue shutdown as normal. func (a *App) beforeClose(ctx context.Context) (prevent bool) { + defer PanicHandler() dialog, err := runtime.MessageDialog(ctx, runtime.MessageDialogOptions{ Type: runtime.QuestionDialog, @@ -344,6 +351,7 @@ func (a *App) beforeClose(ctx context.Context) (prevent bool) { // shutdown is called at application termination func (a *App) shutdown(ctx context.Context) { + defer PanicHandler() // Perform your teardown here systray.Quit() } diff --git a/backend/data/openai_api.go b/backend/data/openai_api.go index 11583e5..8124115 100644 --- a/backend/data/openai_api.go +++ b/backend/data/openai_api.go @@ -76,7 +76,19 @@ type AiResponse struct { func (o OpenAi) NewChatStream(stock, stockCode string) <-chan string { ch := make(chan string, 512) + + defer func() { + if err := recover(); err != nil { + logger.SugaredLogger.Error("NewChatStream panic", err) + } + }() + go func() { + defer func() { + if err := recover(); err != nil { + logger.SugaredLogger.Error("NewChatStream goroutine panic", err) + } + }() defer close(ch) msg := []map[string]interface{}{ { @@ -174,7 +186,7 @@ func (o OpenAi) NewChatStream(stock, stockCode string) <-chan string { client.SetBaseURL(o.BaseUrl) client.SetHeader("Authorization", "Bearer "+o.ApiKey) client.SetHeader("Content-Type", "application/json") - client.SetRetryCount(3) + //client.SetRetryCount(3) if o.TimeOut <= 0 { o.TimeOut = 300 } @@ -190,14 +202,15 @@ func (o OpenAi) NewChatStream(stock, stockCode string) <-chan string { }). Post("/chat/completions") - defer resp.RawBody().Close() + body := resp.RawBody() + defer body.Close() if err != nil { logger.SugaredLogger.Infof("Stream error : %s", err.Error()) ch <- err.Error() return } - scanner := bufio.NewScanner(resp.RawBody()) + scanner := bufio.NewScanner(body) for scanner.Scan() { line := scanner.Text() logger.SugaredLogger.Infof("Received data: %s", line) @@ -232,8 +245,13 @@ func (o OpenAi) NewChatStream(stock, stockCode string) <-chan string { } } } else { - logger.SugaredLogger.Infof("Stream data error : %s", err.Error()) - ch <- err.Error() + if err != nil { + logger.SugaredLogger.Infof("Stream data error : %s", err.Error()) + ch <- err.Error() + } else { + logger.SugaredLogger.Infof("Stream data error : %s", data) + ch <- data + } } } else { ch <- line diff --git a/frontend/src/App.vue b/frontend/src/App.vue index ad4b884..cbb7fd9 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -1,6 +1,7 @@