define source error categories
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package source
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestErrorKindThroughWrapping(t *testing.T) {
|
||||
base := errors.New("producer disappeared")
|
||||
wrapped := wrapError("read video", ErrorKindUnavailable, base)
|
||||
outer := fmt.Errorf("worker failed: %w", wrapped)
|
||||
|
||||
if got := errorKind(outer); got != ErrorKindUnavailable {
|
||||
t.Fatalf("errorKind() = %v, want %v", got, ErrorKindUnavailable)
|
||||
}
|
||||
if !errors.Is(outer, base) {
|
||||
t.Fatal("wrapped error does not preserve its cause")
|
||||
}
|
||||
if wrapError("nil", 0, nil) != nil {
|
||||
t.Fatal("nil error is not wrapped as nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrorKindUnknown(t *testing.T) {
|
||||
if got := errorKind(errors.New("ordinary error")); got != ErrorKindUnknown {
|
||||
t.Fatalf("errorKind() = %v, want %v", got, ErrorKindUnknown)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSourceErrorWithOperation(t *testing.T) {
|
||||
err := &SourceError{
|
||||
Op: "read video",
|
||||
Kind: ErrorKindTemporary,
|
||||
Err: errors.New("timeout"),
|
||||
}
|
||||
|
||||
const want = "read video: timeout"
|
||||
if got := err.Error(); got != want {
|
||||
t.Fatalf("Error() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSourceErrorWithoutOperation(t *testing.T) {
|
||||
err := &SourceError{
|
||||
Kind: ErrorKindTemporary,
|
||||
Err: errors.New("timeout"),
|
||||
}
|
||||
|
||||
const want = "timeout"
|
||||
if got := err.Error(); got != want {
|
||||
t.Fatalf("Error() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user