|
| 1 | +package declcfg |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "reflect" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func TestJsonUnmarshalError(t *testing.T) { |
| 14 | + type testCase struct { |
| 15 | + name string |
| 16 | + data []byte |
| 17 | + inErr error |
| 18 | + expectErrorString string |
| 19 | + expectPrettyString string |
| 20 | + } |
| 21 | + validData := []byte(`{"messages": ["Hello", "world!"]}`) |
| 22 | + invalidData := []byte(`{"messages": ["Hello", "world!"]`) |
| 23 | + for _, tc := range []testCase{ |
| 24 | + { |
| 25 | + name: "unknown error", |
| 26 | + data: validData, |
| 27 | + inErr: errors.New("unknown error"), |
| 28 | + expectErrorString: "unknown error", |
| 29 | + expectPrettyString: "unknown error", |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "unmarshal type error: no data", |
| 33 | + data: nil, |
| 34 | + inErr: &json.UnmarshalTypeError{Value: "foo", Type: reflect.TypeOf(""), Offset: 0}, |
| 35 | + expectErrorString: `json: cannot unmarshal foo into Go value of type string`, |
| 36 | + expectPrettyString: `json: cannot unmarshal foo into Go value of type string`, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "unmarshal type error: negative offset", |
| 40 | + data: validData, |
| 41 | + inErr: &json.UnmarshalTypeError{Value: "foo", Type: reflect.TypeOf(""), Offset: -1}, |
| 42 | + expectErrorString: `json: cannot unmarshal foo into Go value of type string`, |
| 43 | + expectPrettyString: `json: cannot unmarshal foo into Go value of type string`, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "unmarshal type error: greater than length", |
| 47 | + data: validData, |
| 48 | + inErr: &json.UnmarshalTypeError{Value: "foo", Type: reflect.TypeOf(""), Offset: int64(len(validData) + 1)}, |
| 49 | + expectErrorString: `json: cannot unmarshal foo into Go value of type string`, |
| 50 | + expectPrettyString: `json: cannot unmarshal foo into Go value of type string`, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "unmarshal type error: offset at beginning", |
| 54 | + data: validData, |
| 55 | + inErr: &json.UnmarshalTypeError{Value: "foo", Type: reflect.TypeOf(""), Offset: 0}, |
| 56 | + expectErrorString: `json: cannot unmarshal foo into Go value of type string`, |
| 57 | + expectPrettyString: `json: cannot unmarshal foo into Go value of type string at offset 0 (indicated by <==) |
| 58 | + <== { |
| 59 | + "messages": [ |
| 60 | + "Hello", |
| 61 | + "world!" |
| 62 | + ] |
| 63 | +}`, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "unmarshal type error: offset at 1", |
| 67 | + data: validData, |
| 68 | + inErr: &json.UnmarshalTypeError{Value: "foo", Type: reflect.TypeOf(""), Offset: 1}, |
| 69 | + expectErrorString: `json: cannot unmarshal foo into Go value of type string`, |
| 70 | + expectPrettyString: `json: cannot unmarshal foo into Go value of type string at offset 1 (indicated by <==) |
| 71 | +{ <== |
| 72 | + "messages": [ |
| 73 | + "Hello", |
| 74 | + "world!" |
| 75 | + ] |
| 76 | +}`, |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "unmarshal type error: offset at end", |
| 80 | + data: validData, |
| 81 | + inErr: &json.UnmarshalTypeError{Value: "foo", Type: reflect.TypeOf(""), Offset: int64(len(validData))}, |
| 82 | + expectErrorString: `json: cannot unmarshal foo into Go value of type string`, |
| 83 | + expectPrettyString: fmt.Sprintf(`json: cannot unmarshal foo into Go value of type string at offset %d (indicated by <==) |
| 84 | +{ |
| 85 | + "messages": [ |
| 86 | + "Hello", |
| 87 | + "world!" |
| 88 | + ] |
| 89 | +} <==`, len(validData)), |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "syntax error: no data", |
| 93 | + data: nil, |
| 94 | + inErr: json.Unmarshal(invalidData, nil), |
| 95 | + expectErrorString: `unexpected end of JSON input`, |
| 96 | + expectPrettyString: `unexpected end of JSON input`, |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "syntax error: negative offset", |
| 100 | + data: invalidData, |
| 101 | + inErr: customOffsetSyntaxError(invalidData, -1), |
| 102 | + expectErrorString: `unexpected end of JSON input`, |
| 103 | + expectPrettyString: `unexpected end of JSON input`, |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "syntax error: greater than length", |
| 107 | + data: invalidData, |
| 108 | + inErr: customOffsetSyntaxError(invalidData, int64(len(invalidData)+1)), |
| 109 | + expectErrorString: `unexpected end of JSON input`, |
| 110 | + expectPrettyString: `unexpected end of JSON input`, |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "syntax error: offset at beginning", |
| 114 | + data: invalidData, |
| 115 | + inErr: customOffsetSyntaxError(invalidData, 0), |
| 116 | + expectErrorString: `unexpected end of JSON input`, |
| 117 | + expectPrettyString: `unexpected end of JSON input at offset 0 (indicated by <==) |
| 118 | + <== {"messages": ["Hello", "world!"]`, |
| 119 | + }, |
| 120 | + { |
| 121 | + name: "syntax error: offset at 1", |
| 122 | + data: invalidData, |
| 123 | + inErr: customOffsetSyntaxError(invalidData, 1), |
| 124 | + expectErrorString: `unexpected end of JSON input`, |
| 125 | + expectPrettyString: `unexpected end of JSON input at offset 1 (indicated by <==) |
| 126 | +{ <== "messages": ["Hello", "world!"]`, |
| 127 | + }, |
| 128 | + { |
| 129 | + name: "syntax error: offset at end", |
| 130 | + data: invalidData, |
| 131 | + inErr: customOffsetSyntaxError(invalidData, int64(len(invalidData))), |
| 132 | + expectErrorString: `unexpected end of JSON input`, |
| 133 | + expectPrettyString: fmt.Sprintf(`unexpected end of JSON input at offset %d (indicated by <==) |
| 134 | +{"messages": ["Hello", "world!"] <==`, len(invalidData)), |
| 135 | + }, |
| 136 | + } { |
| 137 | + t.Run(tc.name, func(t *testing.T) { |
| 138 | + actualErr := newJSONUnmarshalError(tc.data, tc.inErr) |
| 139 | + assert.Equal(t, tc.expectErrorString, actualErr.Error()) |
| 140 | + assert.Equal(t, tc.expectPrettyString, actualErr.Pretty()) |
| 141 | + }) |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +// customOffsetSyntaxError returns a json.SyntaxError with the given offset. |
| 146 | +// json.SyntaxError does not have a public constructor, so we have to use |
| 147 | +// json.Unmarshal to create one and then set the offset manually. |
| 148 | +// |
| 149 | +// If the data does not cause a syntax error, this function will panic. |
| 150 | +func customOffsetSyntaxError(data []byte, offset int64) *json.SyntaxError { |
| 151 | + err := json.Unmarshal(data, nil).(*json.SyntaxError) |
| 152 | + err.Offset = offset |
| 153 | + return err |
| 154 | +} |
0 commit comments