github.com/unionj-cloud/go-doudou@v1.3.8-0.20221011095552-0088008e5b31/framework/http/model/bizerror_test.go (about)

     1  package model_test
     2  
     3  import (
     4  	"github.com/pkg/errors"
     5  	. "github.com/smartystreets/goconvey/convey"
     6  	"github.com/unionj-cloud/go-doudou/framework/http/model"
     7  	"testing"
     8  )
     9  
    10  func TestWithStatusCode(t *testing.T) {
    11  	Convey("Create a BizError with 401 http status code", t, func() {
    12  		bizError := model.NewBizError(errors.New("Unauthorised"), model.WithStatusCode(401))
    13  		So(bizError, ShouldNotBeZeroValue)
    14  		So(bizError.StatusCode, ShouldEqual, 401)
    15  
    16  		Convey("Should output Unauthorised", func() {
    17  			So(bizError.String(), ShouldEqual, "Unauthorised")
    18  		})
    19  	})
    20  }
    21  
    22  func TestWithErrCode(t *testing.T) {
    23  	Convey("Create a BizError with 100401 business error code", t, func() {
    24  		bizError := model.NewBizError(errors.New("Unauthorised"), model.WithErrCode(100401))
    25  		So(bizError, ShouldNotBeZeroValue)
    26  		So(bizError.ErrCode, ShouldEqual, 100401)
    27  
    28  		Convey("Should output 100401 Unauthorised", func() {
    29  			So(bizError.String(), ShouldEqual, "100401 Unauthorised")
    30  		})
    31  
    32  		Convey("Should have the same output with String()", func() {
    33  			So(bizError.Error(), ShouldEqual, bizError.String())
    34  		})
    35  	})
    36  }