github.com/vicanso/hes@v0.7.0/http_errors_test.go (about) 1 package hes 2 3 import ( 4 "errors" 5 "fmt" 6 "strings" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestNewHTTPError(t *testing.T) { 13 he := New("my error") 14 he.Category = "custom" 15 he.Code = "1-101" 16 he.StatusCode = 400 17 t.Run("error", func(t *testing.T) { 18 assert := assert.New(t) 19 assert.Equal("statusCode=400, category=custom, code=1-101, message=my error", he.Error(), "get error message fail") 20 }) 21 22 t.Run("format", func(t *testing.T) { 23 assert := assert.New(t) 24 he := New("my error") 25 assert.Equal("statusCode=400, message=my error", fmt.Sprintf("%s", he)) 26 assert.Equal(`"my error"`, fmt.Sprintf("%q", he)) 27 }) 28 29 t.Run("set caller", func(t *testing.T) { 30 assert := assert.New(t) 31 he.SetCaller(1) 32 assert.NotEmpty(he.File) 33 assert.NotEqual(0, he.Line) 34 }) 35 36 t.Run("new with status code", func(t *testing.T) { 37 assert := assert.New(t) 38 message := "abc" 39 category := "category" 40 he := NewWithStatusCode(message, 403, category) 41 assert.Equal(message, he.Message) 42 assert.Equal(403, he.StatusCode) 43 assert.Equal(category, he.Category) 44 }) 45 46 t.Run("new with error", func(t *testing.T) { 47 assert := assert.New(t) 48 err := errors.New("abcd") 49 he := NewWithError(err) 50 assert.Equal(defaultStatusCode, he.StatusCode) 51 assert.Equal(err, he.Err) 52 assert.Equal(err.Error(), he.Message) 53 }) 54 55 t.Run("check error", func(t *testing.T) { 56 assert := assert.New(t) 57 assert.True(IsError(he)) 58 assert.False(IsError(errors.New("abcd"))) 59 }) 60 61 t.Run("error list", func(t *testing.T) { 62 assert := assert.New(t) 63 he := New("message") 64 he1 := New("messsage1") 65 he.Add(he1, nil) 66 assert.False(he.IsEmpty()) 67 assert.True(he.IsNotEmpty()) 68 assert.Equal("{\"statusCode\":400,\"message\":\"message\",\"errs\":[{\"statusCode\":400,\"message\":\"messsage1\"}]}", string(he.ToJSON())) 69 assert.Equal("statusCode=400, message=message, errs:(statusCode=400, message=messsage1)", he.Error()) 70 71 he2 := New("message2") 72 he2.Add(he) 73 assert.Equal("statusCode=400, message=message2, errs:(statusCode=400, message=messsage1)", he2.Error()) 74 }) 75 76 t.Run("add extra", func(t *testing.T) { 77 assert := assert.New(t) 78 79 he := New("message") 80 he.AddExtra("title", "test") 81 assert.Equal("test", he.Extra["title"]) 82 }) 83 } 84 85 func TestMutexError(t *testing.T) { 86 assert := assert.New(t) 87 he := NewMutex("my error") 88 he.SetCaller(1) 89 assert.NotEmpty(he.File) 90 assert.NotEmpty(he.Line) 91 92 he.Add(errors.New("abc")) 93 94 he1 := he.Clone() 95 assert.Equal(he.Message, he1.Message) 96 } 97 98 func TestNewWithCaller(t *testing.T) { 99 assert := assert.New(t) 100 he := NewWithCaller("my error") 101 assert.NotEmpty(he.File) 102 assert.NotEqual(0, he.Line) 103 } 104 105 func TestNewWithExcpetion(t *testing.T) { 106 assert := assert.New(t) 107 he := NewWithException("abc") 108 assert.True(he.Exception) 109 } 110 111 func TestEnableCaller(t *testing.T) { 112 assert := assert.New(t) 113 EnableCaller(true) 114 SetFileConvertor(func(file string) string { 115 return strings.Replace(file, "hes", "", -1) 116 }) 117 defer EnableCaller(false) 118 119 he := New("test") 120 assert.NotEmpty(he.File) 121 assert.False(strings.Contains(he.File, "hes")) 122 assert.True(strings.Contains(he.File, "http_errors_test.go")) 123 assert.NotEmpty(he.Line) 124 } 125 126 func TestToJSON(t *testing.T) { 127 assert := assert.New(t) 128 he := NewWithCaller("my error") 129 he.Category = "cat" 130 he.Code = "code-001" 131 he.StatusCode = 500 132 he.Exception = true 133 he.Err = errors.New("abcd") 134 he.Extra = map[string]interface{}{ 135 "a": 1, 136 "b": "2", 137 } 138 str := fmt.Sprintf(`{"statusCode":500,"code":"code-001","category":"cat","message":"my error","exception":true,"file":"%s","line":%d,"extra":{"a":1,"b":"2"}}`, he.File, he.Line) 139 assert.Equal(str, string(he.ToJSON())) 140 } 141 142 func TestClone(t *testing.T) { 143 assert := assert.New(t) 144 he := NewWithErrorStatusCode(errors.New("abc"), 400) 145 heClone := he.CloneWithMessage("def") 146 assert.NotEqual(he, heClone) 147 assert.NotEqual(he.Message, heClone.Message) 148 assert.Equal("def", heClone.Message) 149 150 heClone = he.Clone() 151 assert.Equal(he.Message, heClone.Message) 152 he.Message = he.Message + "xx" 153 assert.NotEqual(he.Message, heClone.Message) 154 } 155 156 func TestABC(t *testing.T) { 157 assert := assert.New(t) 158 he := &Error{ 159 Message: "error message", 160 Code: "cus-validate-fail", 161 Category: "common", 162 } 163 assert.Equal("category=common, code=cus-validate-fail, message=error message", fmt.Sprintf("%s", he)) 164 } 165 166 func TestWrap(t *testing.T) { 167 assert := assert.New(t) 168 he := &Error{ 169 Message: "error message", 170 } 171 assert.Equal(he, Wrap(he)) 172 173 err := errors.New("abcd") 174 he = Wrap(err) 175 176 assert.Equal(err, he.Err) 177 assert.Equal(err.Error(), he.Message) 178 }