26 lines
808 B
Go
26 lines
808 B
Go
package middleware_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
|
|
"git.yuntuo.hk/k2safe/go-omni/httpx"
|
|
"git.yuntuo.hk/k2safe/go-omni/middleware"
|
|
"testing"
|
|
)
|
|
|
|
func TestContextErrorsPreservesExplicitBusinessFailures(t *testing.T) {
|
|
business := apierrors.New(409, "CONFLICT", "conflict").WithCause(context.DeadlineExceeded)
|
|
for _, tc := range []struct {
|
|
err error
|
|
code string
|
|
}{{context.Canceled, "CANCELED"}, {fmt.Errorf("operation: %w", context.DeadlineExceeded), "TIMEOUT"}, {business, "CONFLICT"}} {
|
|
err := middleware.ContextErrors("CANCELED", "TIMEOUT")(func(*httpx.Context) error { return tc.err })(nil)
|
|
var result *apierrors.APIError
|
|
if !errors.As(err, &result) || result.Code != tc.code || !errors.Is(err, tc.err) {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|