forked from sashabaranov/go-openai
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move error_accumulator into internal pkg (sashabaranov#304)
- Loading branch information
Showing
10 changed files
with
128 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package openai_test | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"testing" | ||
|
||
utils "github.com/sashabaranov/go-openai/internal" | ||
"github.com/sashabaranov/go-openai/internal/test" | ||
) | ||
|
||
func TestErrorAccumulatorBytes(t *testing.T) { | ||
accumulator := &utils.DefaultErrorAccumulator{ | ||
Buffer: &bytes.Buffer{}, | ||
} | ||
|
||
errBytes := accumulator.Bytes() | ||
if len(errBytes) != 0 { | ||
t.Fatalf("Did not return nil with empty bytes: %s", string(errBytes)) | ||
} | ||
|
||
err := accumulator.Write([]byte("{}")) | ||
if err != nil { | ||
t.Fatalf("%+v", err) | ||
} | ||
|
||
errBytes = accumulator.Bytes() | ||
if len(errBytes) == 0 { | ||
t.Fatalf("Did not return error bytes when has error: %s", string(errBytes)) | ||
} | ||
} | ||
|
||
func TestErrorByteWriteErrors(t *testing.T) { | ||
accumulator := &utils.DefaultErrorAccumulator{ | ||
Buffer: &test.FailingErrorBuffer{}, | ||
} | ||
err := accumulator.Write([]byte("{")) | ||
if !errors.Is(err, test.ErrTestErrorAccumulatorWriteFailed) { | ||
t.Fatalf("Did not return error when write failed: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package test | ||
|
||
import "errors" | ||
|
||
var ( | ||
ErrTestErrorAccumulatorWriteFailed = errors.New("test error accumulator failed") | ||
) | ||
|
||
type FailingErrorBuffer struct{} | ||
|
||
func (b *FailingErrorBuffer) Write(_ []byte) (n int, err error) { | ||
return 0, ErrTestErrorAccumulatorWriteFailed | ||
} | ||
|
||
func (b *FailingErrorBuffer) Len() int { | ||
return 0 | ||
} | ||
|
||
func (b *FailingErrorBuffer) Bytes() []byte { | ||
return []byte{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters