mapStreamError converts a mid-stream upstream error or processing error into a relayable ResponseError. Returns nil when the error is unrecoverable, in which case nothing can be relayed back.
(ctx context.Context, logger slog.Logger, streamErr, lastErr error)
| 607 | // when the error is unrecoverable, in which case nothing can be |
| 608 | // relayed back. |
| 609 | func (*StreamingInterception) mapStreamError(ctx context.Context, logger slog.Logger, streamErr, lastErr error) *ResponseError { |
| 610 | if streamErr != nil { |
| 611 | if eventstream.IsUnrecoverableError(streamErr) { |
| 612 | logger.Debug(ctx, "stream terminated", slog.Error(streamErr)) |
| 613 | // We can't reflect an error back if there's a connection error or the request context was canceled. |
| 614 | return nil |
| 615 | } |
| 616 | if antErr := responseErrorFromAPIError(streamErr); antErr != nil { |
| 617 | logger.Warn(ctx, "anthropic stream error", slog.Error(streamErr)) |
| 618 | return antErr |
| 619 | } |
| 620 | logger.Warn(ctx, "unknown stream error", slog.Error(streamErr)) |
| 621 | // Unfortunately, the Anthropic SDK does not support parsing errors received in the stream |
| 622 | // into known types (i.e. [shared.OverloadedError]). |
| 623 | // See https://github.com/anthropics/anthropic-sdk-go/blob/v1.12.0/packages/ssestream/ssestream.go#L172-L174 |
| 624 | // All it does is wrap the payload in an error - which is all we can return, currently. |
| 625 | return newResponseError(fmt.Sprintf("unknown stream error: %s", streamErr), string(constant.ValueOf[constant.Error]()), http.StatusBadGateway, 0) |
| 626 | } |
| 627 | if lastErr != nil { |
| 628 | logger.Warn(ctx, "stream processing failed", slog.Error(lastErr)) |
| 629 | return newResponseError(fmt.Sprintf("processing error: %s", lastErr), string(constant.ValueOf[constant.Error]()), http.StatusBadGateway, 0) |
| 630 | } |
| 631 | return nil |
| 632 | } |
| 633 | |
| 634 | func (i *StreamingInterception) marshalEvent(event anthropic.MessageStreamEventUnion) ([]byte, error) { |
| 635 | sj, err := sjson.Set(event.RawJSON(), "message.id", i.ID().String()) |
no test coverage detected