* move error_accumulator into internal pkg (#304) * move error_accumulator into internal pkg (#304) * add a test for ErrTooManyEmptyStreamMessages in stream_reader (#304)
This commit is contained in:
committed by
GitHub
parent
fa694c61c2
commit
1394329e44
44
internal/error_accumulator.go
Normal file
44
internal/error_accumulator.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package openai
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
type ErrorAccumulator interface {
|
||||
Write(p []byte) error
|
||||
Bytes() []byte
|
||||
}
|
||||
|
||||
type errorBuffer interface {
|
||||
io.Writer
|
||||
Len() int
|
||||
Bytes() []byte
|
||||
}
|
||||
|
||||
type DefaultErrorAccumulator struct {
|
||||
Buffer errorBuffer
|
||||
}
|
||||
|
||||
func NewErrorAccumulator() ErrorAccumulator {
|
||||
return &DefaultErrorAccumulator{
|
||||
Buffer: &bytes.Buffer{},
|
||||
}
|
||||
}
|
||||
|
||||
func (e *DefaultErrorAccumulator) Write(p []byte) error {
|
||||
_, err := e.Buffer.Write(p)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error accumulator write error, %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *DefaultErrorAccumulator) Bytes() (errBytes []byte) {
|
||||
if e.Buffer.Len() == 0 {
|
||||
return
|
||||
}
|
||||
errBytes = e.Buffer.Bytes()
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user