Skip to content

Commit

Permalink
fix(bedrock): handle exceptions messages in bedrock stream
Browse files Browse the repository at this point in the history
  • Loading branch information
yjp20 authored and stainless-app[bot] committed Dec 19, 2024
1 parent df35e40 commit 234d010
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion bedrock/bedrock.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,41 @@ func (e *eventstreamDecoder) Next() bool {
}

case eventstreamapi.ExceptionMessageType:
// See https://github.com/aws/aws-sdk-go-v2/blob/885de40869f9bcee29ad11d60967aa0f1b571d46/service/iotsitewise/deserializers.go#L15511C1-L15567C2
exceptionType := msg.Headers.Get(eventstreamapi.ExceptionTypeHeader)
if exceptionType == nil {
e.err = fmt.Errorf("%s event header not present", eventstreamapi.ExceptionTypeHeader)
return false
}

// See https://github.com/aws/aws-sdk-go-v2/blob/885de40869f9bcee29ad11d60967aa0f1b571d46/aws/protocol/restjson/decoder_util.go#L15-L48k
var errInfo struct {
Code string
Type string `json:"__type"`
Message string
}
err = json.Unmarshal(msg.Payload, &errInfo)
if err != nil && err != io.EOF {
e.err = fmt.Errorf("received exception %s: parsing exception payload failed: %w", exceptionType.String(), err)
return false
}

errorCode := "UnknownError"
errorMessage := errorCode
if ev := exceptionType.String(); len(ev) > 0 {
errorCode = ev
} else if len(errInfo.Code) > 0 {
errorCode = errInfo.Code
} else if len(errInfo.Type) > 0 {
errorCode = errInfo.Type
}

if len(errInfo.Message) > 0 {
errorMessage = errInfo.Message
}
e.err = fmt.Errorf("received exception %s: %s", errorCode, errorMessage)
return false

case eventstreamapi.ErrorMessageType:
errorCode := "UnknownError"
errorMessage := errorCode
Expand All @@ -106,7 +141,7 @@ func (e *eventstreamDecoder) Next() bool {
if header := msg.Headers.Get(eventstreamapi.ErrorMessageHeader); header != nil {
errorMessage = header.String()
}
e.err = fmt.Errorf("received error or exception %s: %s", errorCode, errorMessage)
e.err = fmt.Errorf("received error %s: %s", errorCode, errorMessage)
return false
}

Expand Down

0 comments on commit 234d010

Please sign in to comment.