github.com/nyan233/littlerpc@v0.4.6-0.20230316182519-0c8d5c48abaf/core/common/errorhandler/errorhandler_test.go (about) 1 package errorhandler 2 3 import ( 4 "encoding/json" 5 "fmt" 6 error2 "github.com/nyan233/littlerpc/core/protocol/error" 7 "github.com/stretchr/testify/assert" 8 "testing" 9 ) 10 11 func TestErrorHandler(t *testing.T) { 12 eh := NewStackTrace() 13 const ( 14 NCall = 32 15 OneSendN = 4 16 ) 17 var err error2.LErrorDesc 18 for i := 0; i < NCall; i++ { 19 sendErrInfo := make([]interface{}, 0, OneSendN) 20 for j := 0; j < OneSendN; j++ { 21 sendErrInfo = append(sendErrInfo, fmt.Sprintf("test%d", j)) 22 } 23 if err == nil { 24 err = eh.LNewErrorDesc(error2.ConnectionErr, "Connection", sendErrInfo...) 25 } else { 26 err = eh.LWarpErrorDesc(err, sendErrInfo...) 27 } 28 } 29 assert.NotNil(t, err) 30 assert.Equal(t, len(err.Mores()), NCall*OneSendN) 31 type ErrorType struct { 32 Mores []interface{} `json:"mores"` 33 } 34 var errValue ErrorType 35 assert.Equal(t, json.Unmarshal([]byte(err.Error()), &errValue), nil) 36 assert.NotNil(t, errValue.Mores) 37 for k, v := range errValue.Mores { 38 stack, ok := v.(map[string]interface{}) 39 if k == len(errValue.Mores)-1 && !ok { 40 t.Fatal("error information no stack trace") 41 } 42 if !ok { 43 continue 44 } 45 stackTrace, ok := stack["stack"] 46 if !ok { 47 t.Fatal("lookup stack map ok buf no stack trace") 48 } 49 for _, stackTraceValue := range stackTrace.([]interface{}) { 50 _, ok := stackTraceValue.(string) 51 if !ok { 52 t.Fatal("stack trace type not string") 53 } 54 } 55 } 56 oldLen := len(err.Mores()) 57 moresBytes, iErr := err.MarshalMores() 58 assert.Nil(t, iErr) 59 assert.Nil(t, err.UnmarshalMores(moresBytes)) 60 assert.Equal(t, len(err.Mores()), oldLen+1) 61 }