50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
package apperr_test
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"messenger/internal/platform/apperr"
|
|
)
|
|
|
|
func TestKindOfReturnsKindForApplicationError(t *testing.T) {
|
|
err := apperr.NotFound("chat not found")
|
|
if got := apperr.KindOf(err); got != apperr.KindNotFound {
|
|
t.Fatalf("KindOf = %v, want KindNotFound", got)
|
|
}
|
|
if err.Error() != "chat not found" {
|
|
t.Fatalf("Error() = %q", err.Error())
|
|
}
|
|
}
|
|
|
|
func TestKindOfSeesThroughWrapping(t *testing.T) {
|
|
err := fmt.Errorf("service: %w", apperr.Conflict("username taken"))
|
|
if got := apperr.KindOf(err); got != apperr.KindConflict {
|
|
t.Fatalf("KindOf = %v, want KindConflict", got)
|
|
}
|
|
}
|
|
|
|
func TestKindOfDefaultsToInternal(t *testing.T) {
|
|
if got := apperr.KindOf(errors.New("boom")); got != apperr.KindInternal {
|
|
t.Fatalf("KindOf = %v, want KindInternal", got)
|
|
}
|
|
if got := apperr.KindOf(nil); got != apperr.KindInternal {
|
|
t.Fatalf("KindOf(nil) = %v, want KindInternal", got)
|
|
}
|
|
}
|
|
|
|
func TestWrapKeepsCause(t *testing.T) {
|
|
cause := errors.New("cause")
|
|
err := apperr.Wrap(apperr.KindInvalid, "bad input", cause)
|
|
if !errors.Is(err, cause) {
|
|
t.Fatal("wrapped error must unwrap to cause")
|
|
}
|
|
if apperr.KindOf(err) != apperr.KindInvalid {
|
|
t.Fatal("wrapped error must keep its kind")
|
|
}
|
|
if err.Error() != "bad input" {
|
|
t.Fatalf("Error() = %q, want public message only", err.Error())
|
|
}
|
|
}
|