Skip to content

Commit

Permalink
generify
Browse files Browse the repository at this point in the history
  • Loading branch information
mjh1 committed Jan 23, 2023
1 parent 5024b49 commit 10426d3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
27 changes: 17 additions & 10 deletions errors/errors.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
package errors

import "fmt"
import (
"errors"
"fmt"
)

func NewMaxTranscodeAttemptsError(cause error) error {
return &MaxTranscodeAttemptsErr{
cause: cause,
func LpError(customErr, cause error) error {
return &LPError{
cause: cause,
customErr: customErr,
}
}

type MaxTranscodeAttemptsErr struct {
cause error
type LPError struct {
cause error
customErr error
}

func (e *MaxTranscodeAttemptsErr) Error() string {
return fmt.Sprintf("hit max transcode attempts: %s", e.cause.Error())
var MaxTranscodeAttempts = errors.New("hit max transcode attempts")

func (e *LPError) Error() string {
return fmt.Sprintf("%s: %s", e.customErr.Error(), e.cause.Error())
}

func (e *MaxTranscodeAttemptsErr) Unwrap() error {
return e.cause
func (e *LPError) Is(err error) bool {
return errors.Is(err, e.cause) || errors.Is(err, e.customErr)
}
5 changes: 2 additions & 3 deletions server/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ func processSegment(ctx context.Context, cxn *rtmpConnection, seg *stream.HLSSeg
}()
}
if len(attempts) == MaxAttempts && err != nil {
err = lperrors.NewMaxTranscodeAttemptsError(err)
err = lperrors.LpError(lperrors.MaxTranscodeAttempts, err)
if monitor.Enabled {
monitor.SegmentTranscodeFailed(ctx, monitor.SegmentTranscodeErrorMaxAttempts, nonce, seg.SeqNo, err, true)
}
Expand Down Expand Up @@ -1582,8 +1582,7 @@ func isNonRetryableError(err error) bool {
return true
}
}
tErr := &lperrors.MaxTranscodeAttemptsErr{}
if errors.As(err, &tErr) {
if errors.Is(err, lperrors.MaxTranscodeAttempts) {
return true
}
return false
Expand Down
3 changes: 1 addition & 2 deletions server/broadcast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -837,8 +837,7 @@ func TestProcessSegment_MaxAttempts(t *testing.T) {
transcodeCalls = 0
_, err = processSegment(context.Background(), cxn, seg, nil)
assert.NotNil(err)
tErr := &lperrors.MaxTranscodeAttemptsErr{}
assert.True(errors.As(err, &tErr))
assert.True(errors.Is(err, lperrors.MaxTranscodeAttempts))
assert.Equal("hit max transcode attempts: UnknownResponse", err.Error())
assert.Equal(1, transcodeCalls, "Segment submission calls did not match")
assert.Len(bsm.trustedPool.sessMap, 1)
Expand Down

0 comments on commit 10426d3

Please sign in to comment.