Skip to content

Commit

Permalink
元のエラーをクライアントに送信する
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexa committed Nov 22, 2024
1 parent da9251a commit 084539d
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
3 changes: 1 addition & 2 deletions amazon_transcribe_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
}

Expand Down
75 changes: 74 additions & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package suzu
import (
"context"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -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() {
Expand All @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion speech_to_text_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 084539d

Please sign in to comment.