Skip to content

Commit

Permalink
Merge pull request #155 from shiguredo/feature/fix-stream-error
Browse files Browse the repository at this point in the history
stream 処理開始後のエラー処理の修正
  • Loading branch information
Hexa authored Jan 11, 2024
2 parents 47bf4ff + da1f7b9 commit 6d5384a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
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

0 comments on commit 6d5384a

Please sign in to comment.