github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/statusx/error_test.go (about)

     1  package statusx_test
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	. "github.com/onsi/gomega"
    11  
    12  	"github.com/machinefi/w3bstream/pkg/depends/kit/statusx"
    13  	"github.com/machinefi/w3bstream/pkg/depends/kit/statusxgen"
    14  	. "github.com/machinefi/w3bstream/pkg/depends/kit/statusxgen/__examples__"
    15  	"github.com/machinefi/w3bstream/pkg/depends/x/pkgx"
    16  )
    17  
    18  func init() {
    19  	cwd, _ := os.Getwd()
    20  	pkg, _ := pkgx.LoadFrom(filepath.Join(cwd, "../statusxgen/__examples__"))
    21  
    22  	g := statusxgen.New(pkg)
    23  
    24  	g.Scan("StatusError")
    25  	g.Output(cwd)
    26  }
    27  
    28  func TestStatusErr(t *testing.T) {
    29  	var (
    30  		unknownSeStr  = "@StatusErr[UnknownError][500000000][unknown error]"
    31  		unauthedSeStr = "@StatusErr[Unauthorized][401999001][Unauthorized]!"
    32  		internalSeStr = "@StatusErr[InternalServerError][500999001][InternalServerError 内部错误]"
    33  		summary       = statusx.NewUnknownErr().Summary()
    34  		se, err       = statusx.ParseStatusErrSummary(summary)
    35  	)
    36  
    37  	g := NewWithT(t)
    38  
    39  	g.Expect(summary).To(Equal(unknownSeStr))
    40  	g.Expect(err).To(BeNil())
    41  	g.Expect(se).To(Equal(statusx.NewUnknownErr()))
    42  	g.Expect(Unauthorized.StatusErr().Summary()).To(Equal(unauthedSeStr))
    43  	g.Expect(InternalServerError.StatusErr().Summary()).To(Equal(internalSeStr))
    44  	g.Expect(Unauthorized.StatusCode()).To(Equal(401))
    45  	g.Expect(Unauthorized.StatusErr().StatusCode()).To(Equal(401))
    46  
    47  	g.Expect(errors.Is(Unauthorized, Unauthorized)).To(BeTrue())
    48  	g.Expect(errors.Is(Unauthorized.StatusErr(), Unauthorized)).To(BeTrue())
    49  	g.Expect(errors.Is(Unauthorized.StatusErr(), Unauthorized.StatusErr())).To(BeTrue())
    50  }
    51  
    52  func ExampleStatusErr() {
    53  	fmt.Println(Unauthorized)
    54  	fmt.Println(statusx.FromErr(nil))
    55  	fmt.Println(statusx.FromErr(fmt.Errorf("unknown")))
    56  	fmt.Println(Unauthorized.StatusErr().WithMsg("msg overwrite"))
    57  	fmt.Println(Unauthorized.StatusErr().WithDesc("desc overwrite"))
    58  	fmt.Println(Unauthorized.StatusErr().DisableErrTalk().EnableErrTalk())
    59  	fmt.Println(Unauthorized.StatusErr().WithID("111"))
    60  	fmt.Println(Unauthorized.StatusErr().AppendSource("service-abc"))
    61  	fmt.Println(Unauthorized.StatusErr().AppendErrorField("header", "Authorization", "missing"))
    62  	fmt.Println(Unauthorized.StatusErr().AppendErrorFields(
    63  		statusx.NewErrorField("query", "key", "missing"),
    64  		statusx.NewErrorField("header", "Authorization", "missing"),
    65  	))
    66  	// Output:
    67  	// []@StatusErr[Unauthorized][401999001][Unauthorized]!
    68  	// <nil>
    69  	// []@StatusErr[UnknownError][500000000][unknown error] unknown
    70  	// []@StatusErr[Unauthorized][401999001][msg overwrite]!
    71  	// []@StatusErr[Unauthorized][401999001][Unauthorized]! desc overwrite
    72  	// []@StatusErr[Unauthorized][401999001][Unauthorized]!
    73  	// []@StatusErr[Unauthorized][401999001][Unauthorized]!
    74  	// [service-abc]@StatusErr[Unauthorized][401999001][Unauthorized]!
    75  	// []@StatusErr[Unauthorized][401999001][Unauthorized]!<Authorization in header - missing>
    76  	// []@StatusErr[Unauthorized][401999001][Unauthorized]!<Authorization in header - missing, key in query - missing>
    77  }