Skip to content

Commit

Permalink
Split Error func from FragmentEvent (#320)
Browse files Browse the repository at this point in the history
Fmt package handled *FragmentEvent as error as it had Error func.
(V1 API returned non-pointer so that fmt package didn't handle it
 as error, but v2 API returns pointer of FragmentEvent.)
Move Error function to FragmentEventError to avoid being handled as
error.
  • Loading branch information
at-wat authored Oct 31, 2022
1 parent a1a089b commit 1985d31
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
4 changes: 2 additions & 2 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ func (p *Provider) putMedia(ctx context.Context, conn *connection, chResp chan *
if err = p.putMediaRaw(ctx, &nopCloser{bytes.NewReader(backup.Bytes())}, chRespRaw, opts); err == nil {
break
}
if fe, ok := err.(*FragmentEvent); ok && opts.fragmentHeadDumpLen > 0 {
if fe, ok := err.(*FragmentEventError); ok && opts.fragmentHeadDumpLen > 0 {
bb := backup.Bytes()
if len(bb) > opts.fragmentHeadDumpLen {
fe.fragmentHead = bb[:opts.fragmentHeadDumpLen]
Expand Down Expand Up @@ -548,7 +548,7 @@ func (p *Provider) putMediaRaw(ctx context.Context, rc io.ReadCloser, chResp cha
go func() {
for fe := range chFE {
if fe.IsError() && err == nil {
chErr <- fe
chErr <- fe.AsError()
}
chResp <- fe
}
Expand Down
8 changes: 4 additions & 4 deletions provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ func TestProvider(t *testing.T) {
// Skip first fragment.
return false
}
fe, ok := err.(*kvm.FragmentEvent)
fe, ok := err.(*kvm.FragmentEventError)
if !ok {
t.Errorf("Expected FragmentEvent, got %T", err)
t.Errorf("Expected FragmentEventError, got %T", err)
return false
}
expectedDump := []byte{
Expand Down Expand Up @@ -218,9 +218,9 @@ func TestProvider(t *testing.T) {
// Skip first fragment.
return false
}
fe, ok := err.(*kvm.FragmentEvent)
fe, ok := err.(*kvm.FragmentEventError)
if !ok {
t.Errorf("Expected FragmentEvent, got %T", err)
t.Errorf("Expected FragmentEventError, got %T", err)
return false
}
expectedDump := []byte{
Expand Down
18 changes: 13 additions & 5 deletions putresp.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,22 @@ func (e *FragmentEvent) IsError() bool {
return e.EventType == "ERROR"
}

func (e *FragmentEvent) Error() string {
func (e *FragmentEvent) AsError() error {
if e.EventType != "ERROR" {
panic("non-error FragmentEvent is used as error")
}
return &FragmentEventError{FragmentEvent: *e}
}

func (e *FragmentEvent) Dump() []byte {
return e.fragmentHead
}

type FragmentEventError struct {
FragmentEvent
}

func (e FragmentEventError) Error() string {
var dump string
if len(e.fragmentHead) > 0 {
dump = `, Data: "` + base64.RawStdEncoding.EncodeToString(e.fragmentHead) + `"`
Expand All @@ -74,10 +86,6 @@ func (e *FragmentEvent) Error() string {
)
}

func (e *FragmentEvent) Dump() []byte {
return e.fragmentHead
}

func parseFragmentEvent(r io.Reader, ch chan *FragmentEvent) error {
defer func() {
close(ch)
Expand Down
4 changes: 2 additions & 2 deletions putresp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ func TestFragmentEvent(t *testing.T) {
}

expected := `fragment event error: { Timecode: 12345, FragmentNumber: 91343852333754009371412493862204112772176002064, ErrorId: 5000, ErrorCode: "DUMMY_ERROR" }`
if s := fe[0].Error(); s != expected {
if s := fe[0].AsError().Error(); s != expected {
t.Errorf("Expected error string:\n%s\ngot:\n%s", expected, s)
}

fe[0].fragmentHead = []byte("test")

expected2 := `fragment event error: { Timecode: 12345, FragmentNumber: 91343852333754009371412493862204112772176002064, ErrorId: 5000, ErrorCode: "DUMMY_ERROR", Data: "dGVzdA" }`
if s := fe[0].Error(); s != expected2 {
if s := fe[0].AsError().Error(); s != expected2 {
t.Errorf("Expected error string:\n%s\ngot:\n%s", expected2, s)
}
})
Expand Down

0 comments on commit 1985d31

Please sign in to comment.