github.com/vnforks/kid@v5.11.1+incompatible/utils/jsonutils/json_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package jsonutils_test 5 6 import ( 7 "encoding/json" 8 "reflect" 9 "testing" 10 11 "github.com/pkg/errors" 12 "github.com/stretchr/testify/assert" 13 14 "github.com/mattermost/mattermost-server/utils/jsonutils" 15 ) 16 17 func TestHumanizeJsonError(t *testing.T) { 18 t.Parallel() 19 20 type testType struct{} 21 22 testCases := []struct { 23 Description string 24 Data []byte 25 Err error 26 ExpectedErr string 27 }{ 28 { 29 "nil error", 30 []byte{}, 31 nil, 32 "", 33 }, 34 { 35 "non-special error", 36 []byte{}, 37 errors.New("test"), 38 "test", 39 }, 40 { 41 "syntax error, offset 17, middle of line 3", 42 []byte("line 1\nline 2\nline 3"), 43 &json.SyntaxError{ 44 // msg can't be set 45 Offset: 17, 46 }, 47 "parsing error at line 3, character 4: ", 48 }, 49 { 50 "unmarshal type error, offset 17, middle of line 3", 51 []byte("line 1\nline 2\nline 3"), 52 &json.UnmarshalTypeError{ 53 Value: "bool", 54 Type: reflect.TypeOf(testType{}), 55 Offset: 17, 56 Struct: "struct", 57 Field: "field", 58 }, 59 "parsing error at line 3, character 4: json: cannot unmarshal bool into Go struct field struct.field of type jsonutils_test.testType", 60 }, 61 } 62 63 for _, testCase := range testCases { 64 testCase := testCase 65 t.Run(testCase.Description, func(t *testing.T) { 66 actual := jsonutils.HumanizeJsonError(testCase.Err, testCase.Data) 67 if testCase.ExpectedErr == "" { 68 assert.NoError(t, actual) 69 } else { 70 assert.EqualError(t, actual, testCase.ExpectedErr) 71 } 72 }) 73 } 74 } 75 76 func TestNewHumanizedJsonError(t *testing.T) { 77 t.Parallel() 78 79 testCases := []struct { 80 Description string 81 Data []byte 82 Offset int64 83 Err error 84 Expected *jsonutils.HumanizedJsonError 85 }{ 86 { 87 "nil error", 88 []byte{}, 89 0, 90 nil, 91 nil, 92 }, 93 { 94 "offset -1, before start of string", 95 []byte("line 1\nline 2\nline 3"), 96 -1, 97 errors.New("message"), 98 &jsonutils.HumanizedJsonError{ 99 Err: errors.Wrap(errors.New("message"), "invalid offset -1"), 100 }, 101 }, 102 { 103 "offset 0, start of string", 104 []byte("line 1\nline 2\nline 3"), 105 0, 106 errors.New("message"), 107 &jsonutils.HumanizedJsonError{ 108 Err: errors.Wrap(errors.New("message"), "parsing error at line 1, character 1"), 109 Line: 1, 110 Character: 1, 111 }, 112 }, 113 { 114 "offset 5, end of line 1", 115 []byte("line 1\nline 2\nline 3"), 116 5, 117 errors.New("message"), 118 &jsonutils.HumanizedJsonError{ 119 Err: errors.Wrap(errors.New("message"), "parsing error at line 1, character 6"), 120 Line: 1, 121 Character: 6, 122 }, 123 }, 124 { 125 "offset 6, new line at end end of line 1", 126 []byte("line 1\nline 2\nline 3"), 127 6, 128 errors.New("message"), 129 &jsonutils.HumanizedJsonError{ 130 Err: errors.Wrap(errors.New("message"), "parsing error at line 1, character 7"), 131 Line: 1, 132 Character: 7, 133 }, 134 }, 135 { 136 "offset 7, start of line 2", 137 []byte("line 1\nline 2\nline 3"), 138 7, 139 errors.New("message"), 140 &jsonutils.HumanizedJsonError{ 141 Err: errors.Wrap(errors.New("message"), "parsing error at line 2, character 1"), 142 Line: 2, 143 Character: 1, 144 }, 145 }, 146 { 147 "offset 12, end of line 2", 148 []byte("line 1\nline 2\nline 3"), 149 12, 150 errors.New("message"), 151 &jsonutils.HumanizedJsonError{ 152 Err: errors.Wrap(errors.New("message"), "parsing error at line 2, character 6"), 153 Line: 2, 154 Character: 6, 155 }, 156 }, 157 { 158 "offset 13, newline at end of line 2", 159 []byte("line 1\nline 2\nline 3"), 160 13, 161 errors.New("message"), 162 &jsonutils.HumanizedJsonError{ 163 Err: errors.Wrap(errors.New("message"), "parsing error at line 2, character 7"), 164 Line: 2, 165 Character: 7, 166 }, 167 }, 168 { 169 "offset 17, middle of line 3", 170 []byte("line 1\nline 2\nline 3"), 171 17, 172 errors.New("message"), 173 &jsonutils.HumanizedJsonError{ 174 Err: errors.Wrap(errors.New("message"), "parsing error at line 3, character 4"), 175 Line: 3, 176 Character: 4, 177 }, 178 }, 179 { 180 "offset 19, end of string", 181 []byte("line 1\nline 2\nline 3"), 182 19, 183 errors.New("message"), 184 &jsonutils.HumanizedJsonError{ 185 Err: errors.Wrap(errors.New("message"), "parsing error at line 3, character 6"), 186 Line: 3, 187 Character: 6, 188 }, 189 }, 190 { 191 "offset 20, offset = length of string", 192 []byte("line 1\nline 2\nline 3"), 193 20, 194 errors.New("message"), 195 &jsonutils.HumanizedJsonError{ 196 Err: errors.Wrap(errors.New("message"), "parsing error at line 3, character 7"), 197 Line: 3, 198 Character: 7, 199 }, 200 }, 201 { 202 "offset 21, offset = length of string, after newline", 203 []byte("line 1\nline 2\nline 3\n"), 204 21, 205 errors.New("message"), 206 &jsonutils.HumanizedJsonError{ 207 Err: errors.Wrap(errors.New("message"), "parsing error at line 4, character 1"), 208 Line: 4, 209 Character: 1, 210 }, 211 }, 212 { 213 "offset 21, offset > length of string", 214 []byte("line 1\nline 2\nline 3"), 215 21, 216 errors.New("message"), 217 &jsonutils.HumanizedJsonError{ 218 Err: errors.Wrap(errors.New("message"), "invalid offset 21"), 219 }, 220 }, 221 } 222 223 for _, testCase := range testCases { 224 testCase := testCase 225 t.Run(testCase.Description, func(t *testing.T) { 226 actual := jsonutils.NewHumanizedJsonError(testCase.Err, testCase.Data, testCase.Offset) 227 if testCase.Expected != nil && actual.Err != nil { 228 if assert.EqualValues(t, testCase.Expected.Err.Error(), actual.Err.Error()) { 229 actual.Err = testCase.Expected.Err 230 } 231 } 232 assert.Equal(t, testCase.Expected, actual) 233 }) 234 } 235 }