Skip to content

Commit 68320b9

Browse files
authored
Merge pull request #45 from hughdanliu/improper-parameter-logging
Improve error logging when users provide improperly formatted parameters
2 parents 8ce14b4 + 9272f16 commit 68320b9

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

pkg/util/util.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,10 @@ func ConvertJsonStringToObject(input string, object any) error {
138138
return nil
139139
}
140140

141-
// ConvertObjectType converts a given object into another object.
142-
// The to object must be passed in by reference
141+
// ConvertObjectType converts a given object into another object type.
142+
// This is done by converting the object into a JSON string, then using the
143+
// JSON string to populate the parameters of a new object.
144+
// The "to" object must be passed in by reference
143145
func ConvertObjectType(from any, to any) error {
144146
fromString, err := ConvertObjectToJsonString(from)
145147
if err != nil {
@@ -158,6 +160,9 @@ func ConvertObjectType(from any, to any) error {
158160
func RemoveParametersAndPopulateObject(parameters map[string]string, config any) error {
159161
from := ConvertStringMapToAny(parameters)
160162
err := ConvertObjectType(from, config)
163+
if err != nil && strings.Contains(err.Error(), "error calling MarshalJSON") {
164+
return identifyImproperlyFormattedParameter(from)
165+
}
161166

162167
for i := 0; i < reflect.TypeOf(config).Elem().NumField(); i++ {
163168
name := reflect.TypeOf(config).Elem().Field(i).Name
@@ -210,3 +215,20 @@ func MapCopy(oldMap map[string]string) map[string]string {
210215
}
211216
return newMap
212217
}
218+
219+
// identifyImproperlyFormattedParameter is called if the JSON marshal fails to parse the parameter map.
220+
// It attempts to convert each individual parameter into a JSON string to identify the specific
221+
// parameter that is improperly formatted on the storage class. This allows us to provide a more
222+
// granular error message to the user.
223+
func identifyImproperlyFormattedParameter(parameters map[string]any) error {
224+
for key, value := range parameters {
225+
parameter := map[string]any{
226+
key: value,
227+
}
228+
_, err := ConvertObjectToJsonString(parameter)
229+
if err != nil {
230+
return fmt.Errorf("failed to parse parameter key=%s, value=%s with error=%s", key, value, err)
231+
}
232+
}
233+
return fmt.Errorf("failed to parse parameter JSON, but could not determine which parameter could not be parsed")
234+
}

pkg/util/util_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"encoding/json"
2020
"fmt"
2121
"reflect"
22+
"strings"
2223
"testing"
2324
)
2425

@@ -247,3 +248,45 @@ func TestConvertJsonStringToObject(t *testing.T) {
247248
})
248249
}
249250
}
251+
252+
func TestIdentifyImproperlyFormattedParameter(t *testing.T) {
253+
testCases := []struct {
254+
name string
255+
input map[string]string
256+
error string
257+
}{
258+
{
259+
name: "Success: Improperly formatted SubnetIds",
260+
input: map[string]string{
261+
"DeploymentType": "\"SINGLE_AZ_1\"",
262+
"ThroughputCapacity": "64",
263+
"SubnetIds": "[\"subnet-016affca9638a1e61\"",
264+
"SkipFinalBackupOnDeletion": "true",
265+
},
266+
error: "failed to parse parameter key=SubnetIds, value=[\"subnet-016affca9638a1e61\"",
267+
},
268+
{
269+
name: "Success: Unable to identify improperly formatted parameter",
270+
input: map[string]string{
271+
"DeploymentType": "\"SINGLE_AZ_1\"",
272+
"ThroughputCapacity": "64",
273+
"SubnetIds": "[\"subnet-016affca9638a1e61\"]",
274+
"SkipFinalBackupOnDeletion": "true",
275+
},
276+
error: "failed to parse parameter JSON, but could not determine which parameter could not be parsed",
277+
},
278+
}
279+
280+
for _, tc := range testCases {
281+
t.Run(tc.name, func(t *testing.T) {
282+
err := identifyImproperlyFormattedParameter(ConvertStringMapToAny(tc.input))
283+
if err == nil {
284+
t.Fatalf(FailureExpectedError, "identifyImproperlyFormattedParameter")
285+
}
286+
287+
if !strings.Contains(err.Error(), tc.error) {
288+
t.Fatalf(FailureWrongResult, "identifyImproperlyFormattedParameter", err.Error(), tc.error)
289+
}
290+
})
291+
}
292+
}

0 commit comments

Comments
 (0)