expose source error classification
This commit is contained in:
@@ -31,7 +31,9 @@ func (e *SourceError) Unwrap() error {
|
|||||||
return e.Err
|
return e.Err
|
||||||
}
|
}
|
||||||
|
|
||||||
func errorKind(err error) ErrorKind {
|
// KindOf returns the source error category contained in err.
|
||||||
|
// It returns ErrorKindUnknown when err has no SourceError in its chain.
|
||||||
|
func KindOf(err error) ErrorKind {
|
||||||
var sourceErr *SourceError
|
var sourceErr *SourceError
|
||||||
if errors.As(err, &sourceErr) {
|
if errors.As(err, &sourceErr) {
|
||||||
return sourceErr.Kind
|
return sourceErr.Kind
|
||||||
|
|||||||
@@ -6,12 +6,12 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestErrorKindThroughWrapping(t *testing.T) {
|
func TestKindOfThroughWrapping(t *testing.T) {
|
||||||
base := errors.New("producer disappeared")
|
base := errors.New("producer disappeared")
|
||||||
wrapped := wrapError("read video", ErrorKindUnavailable, base)
|
wrapped := wrapError("read video", ErrorKindUnavailable, base)
|
||||||
outer := fmt.Errorf("worker failed: %w", wrapped)
|
outer := fmt.Errorf("worker failed: %w", wrapped)
|
||||||
|
|
||||||
if got := errorKind(outer); got != ErrorKindUnavailable {
|
if got := KindOf(outer); got != ErrorKindUnavailable {
|
||||||
t.Fatalf("errorKind() = %v, want %v", got, ErrorKindUnavailable)
|
t.Fatalf("errorKind() = %v, want %v", got, ErrorKindUnavailable)
|
||||||
}
|
}
|
||||||
if !errors.Is(outer, base) {
|
if !errors.Is(outer, base) {
|
||||||
@@ -22,8 +22,8 @@ func TestErrorKindThroughWrapping(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestErrorKindUnknown(t *testing.T) {
|
func TestKindOfUnknown(t *testing.T) {
|
||||||
if got := errorKind(errors.New("ordinary error")); got != ErrorKindUnknown {
|
if got := KindOf(errors.New("ordinary error")); got != ErrorKindUnknown {
|
||||||
t.Fatalf("errorKind() = %v, want %v", got, ErrorKindUnknown)
|
t.Fatalf("errorKind() = %v, want %v", got, ErrorKindUnknown)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user