Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream 処理開始後のエラー処理の修正 #155

Merged
merged 2 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func (s *Server) createSpeechHandler(serviceType string, onResultFunc func(conte
Msg("CONNECTED")

c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
// すぐにヘッダを送信したい場合はここで c.Response().Flush() を実行する

ctx := c.Request().Context()
// TODO: context.WithCancelCause(ctx) に変更する
Expand Down Expand Up @@ -153,7 +154,7 @@ func (s *Server) createSpeechHandler(serviceType string, onResultFunc func(conte
Str("channel_id", h.SoraChannelID).
Str("connection_id", h.SoraConnectionID).
Send()
return echo.NewHTTPError(499)
return err
} else if errors.Is(err, ErrServerDisconnected) {
if *s.config.Retry {
// サーバから切断されたが再度接続できる可能性があるため、接続を試みる
Expand All @@ -173,7 +174,7 @@ func (s *Server) createSpeechHandler(serviceType string, onResultFunc func(conte
Str("channel_id", h.SoraChannelID).
Str("connection_id", h.SoraConnectionID).
Send()
return echo.NewHTTPError(http.StatusInternalServerError)
return err
}
}

Expand All @@ -182,8 +183,8 @@ func (s *Server) createSpeechHandler(serviceType string, onResultFunc func(conte
Str("channel_id", h.SoraChannelID).
Str("connection_id", h.SoraConnectionID).
Send()
// サーバから切断されたが再度の接続が期待できない場合、または、想定外のエラーの場合は InternalServerError
return echo.NewHTTPError(http.StatusInternalServerError)
// サーバから切断されたが再度の接続が期待できない場合
return err
}

// メッセージが空でない場合はクライアントに結果を送信する
Expand All @@ -194,7 +195,7 @@ func (s *Server) createSpeechHandler(serviceType string, onResultFunc func(conte
Str("channel_id", h.SoraChannelID).
Str("connection_id", h.SoraConnectionID).
Send()
return echo.NewHTTPError(http.StatusInternalServerError)
return err
}
c.Response().Flush()
}
Expand Down
4 changes: 2 additions & 2 deletions test_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func TestSpeechHandler(t *testing.T) {
h := s.createSpeechHandler(serviceType, nil)
err = h(c)
if assert.Error(t, err) {
assert.Equal(t, http.StatusInternalServerError, err.(*echo.HTTPError).Code)
assert.Equal(t, "packet read error", err.Error())
}

pw.Close()
Expand Down Expand Up @@ -354,7 +354,7 @@ func TestSpeechHandler(t *testing.T) {
})
err := h(c)
if assert.Error(t, err) {
assert.Equal(t, http.StatusInternalServerError, err.(*echo.HTTPError).Code)
assert.Equal(t, "ON-RESULT-ERROR", err.Error())
}

})
Expand Down