From 084539d4acc22627e3dc0e9d3005e31a602e6dd4 Mon Sep 17 00:00:00 2001 From: Yoshida Hiroshi Date: Fri, 22 Nov 2024 16:40:40 +0900 Subject: [PATCH] =?UTF-8?q?=E5=85=83=E3=81=AE=E3=82=A8=E3=83=A9=E3=83=BC?= =?UTF-8?q?=E3=82=92=E3=82=AF=E3=83=A9=E3=82=A4=E3=82=A2=E3=83=B3=E3=83=88?= =?UTF-8?q?=E3=81=AB=E9=80=81=E4=BF=A1=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- amazon_transcribe_handler.go | 3 +- handler.go | 75 +++++++++++++++++++++++++++++++++++- speech_to_text_handler.go | 3 +- 3 files changed, 77 insertions(+), 4 deletions(-) diff --git a/amazon_transcribe_handler.go b/amazon_transcribe_handler.go index b9d375c..0093266 100644 --- a/amazon_transcribe_handler.go +++ b/amazon_transcribe_handler.go @@ -198,8 +198,7 @@ func (h *AmazonTranscribeHandler) Handle(ctx context.Context, reader io.Reader) switch err.(type) { case *transcribestreamingservice.LimitExceededException, *transcribestreamingservice.InternalFailureException: - // TODO: 元の err を送信する - err = ErrServerDisconnected + err = errors.Join(err, ErrServerDisconnected) default: } diff --git a/handler.go b/handler.go index c119723..0250279 100644 --- a/handler.go +++ b/handler.go @@ -3,6 +3,7 @@ package suzu import ( "context" "encoding/binary" + "encoding/json" "errors" "fmt" "io" @@ -184,14 +185,39 @@ func (s *Server) createSpeechHandler(serviceType string, onResultFunc func(conte Send() return err } else if errors.Is(err, ErrServerDisconnected) { + errs := err.(interface{ Unwrap() []error }).Unwrap() + // 元の err を取得する + err := errs[0] + if s.config.MaxRetry < 1 { // サーバから切断されたが再接続させない設定の場合 zlog.Error(). + Err(ErrServerDisconnected). Err(err). Str("channel_id", h.SoraChannelID). Str("connection_id", h.SoraConnectionID). Send() - return err + + errMessage, err := json.Marshal(NewSuzuErrorResponse(err)) + if err != nil { + zlog.Error(). + Err(err). + Str("channel_id", h.SoraChannelID). + Str("connection_id", h.SoraConnectionID). + Send() + return err + } + + if _, err := c.Response().Write(errMessage); err != nil { + zlog.Error(). + Err(err). + Str("channel_id", h.SoraChannelID). + Str("connection_id", h.SoraConnectionID). + Send() + return err + } + c.Response().Flush() + return ErrServerDisconnected } if s.config.MaxRetry > serviceHandler.GetRetryCount() { @@ -214,11 +240,58 @@ func (s *Server) createSpeechHandler(serviceType string, onResultFunc func(conte Str("channel_id", h.SoraChannelID). Str("connection_id", h.SoraConnectionID). Send() + + errMessage, err := json.Marshal(NewSuzuErrorResponse(err)) + if err != nil { + zlog.Error(). + Err(err). + Str("channel_id", h.SoraChannelID). + Str("connection_id", h.SoraConnectionID). + Send() + return err + } + + if _, err := c.Response().Write(errMessage); err != nil { + zlog.Error(). + Err(err). + Str("channel_id", h.SoraChannelID). + Str("connection_id", h.SoraConnectionID). + Send() + return err + } + c.Response().Flush() + // max_retry を超えた場合は終了 return c.NoContent(http.StatusOK) } } + zlog.Debug(). + Err(err). + Str("channel_id", h.SoraChannelID). + Str("connection_id", h.SoraConnectionID). + Send() + + errMessage, err := json.Marshal(NewSuzuErrorResponse(err)) + if err != nil { + zlog.Error(). + Err(err). + Str("channel_id", h.SoraChannelID). + Str("connection_id", h.SoraConnectionID). + Send() + return err + } + + if _, err := c.Response().Write(errMessage); err != nil { + zlog.Error(). + Err(err). + Str("channel_id", h.SoraChannelID). + Str("connection_id", h.SoraConnectionID). + Send() + return err + } + c.Response().Flush() + // サーバから切断されたが再度の接続が期待できない場合 return err } diff --git a/speech_to_text_handler.go b/speech_to_text_handler.go index 7141f9d..0a55b9d 100644 --- a/speech_to_text_handler.go +++ b/speech_to_text_handler.go @@ -166,7 +166,8 @@ func (h *SpeechToTextHandler) Handle(ctx context.Context, reader io.Reader) (*io code == codes.InvalidArgument || code == codes.ResourceExhausted { - w.CloseWithError(ErrServerDisconnected) + err := errors.Join(err, ErrServerDisconnected) + w.CloseWithError(err) return }