58 lines
1.5 KiB
Go
58 lines
1.5 KiB
Go
package errs
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
)
|
|
|
|
func TestAppErrorIsAndWith(t *testing.T) {
|
|
err := ErrNotFound.WithMsg("user gone").WithInnerMsg("uid=9")
|
|
if !errors.Is(err, ErrNotFound) {
|
|
t.Fatal("Is by code")
|
|
}
|
|
if errors.Is(err, ErrUnauthorized) {
|
|
t.Fatal("different code")
|
|
}
|
|
if err.Msg != "user gone" || ErrNotFound.Msg == "user gone" {
|
|
t.Fatal("WithMsg must copy")
|
|
}
|
|
code, msg, inner := err.CodeMsg()
|
|
if code != "40401" || msg != "user gone" || inner != "uid=9" {
|
|
t.Fatalf("CodeMsg %s %s %s", code, msg, inner)
|
|
}
|
|
}
|
|
|
|
func TestFromRPCError(t *testing.T) {
|
|
if FromRPCError("s", nil) != nil {
|
|
t.Fatal("nil")
|
|
}
|
|
if got := FromRPCError("s", ErrBusy); !errors.Is(got, ErrBusy) {
|
|
t.Fatal("passthrough")
|
|
}
|
|
if got := FromRPCError("user", context.DeadlineExceeded); !errors.Is(got, ErrTimeout) {
|
|
t.Fatal("timeout")
|
|
}
|
|
if got := FromRPCError("user", errors.New("boom")); !errors.Is(got, ErrInner) {
|
|
t.Fatal("inner")
|
|
}
|
|
}
|
|
|
|
func TestFromDependencyError(t *testing.T) {
|
|
if FromDependencyError("s", nil) != nil {
|
|
t.Fatal("nil")
|
|
}
|
|
if got := FromDependencyError("s", ErrBusy); !errors.Is(got, ErrBusy) {
|
|
t.Fatal("passthrough")
|
|
}
|
|
if got := FromDependencyError("jwt-denylist", context.Canceled); !errors.Is(got, ErrClosed) {
|
|
t.Fatal("canceled")
|
|
}
|
|
if got := FromDependencyError("jwt-denylist", context.DeadlineExceeded); !errors.Is(got, ErrTimeout) {
|
|
t.Fatal("timeout")
|
|
}
|
|
if got := FromDependencyError("jwt-denylist", errors.New("conn refused")); !errors.Is(got, ErrAvailable) {
|
|
t.Fatal("available")
|
|
}
|
|
}
|