cmd/commands: don't error out on replacement failure

This commit is contained in:
Oliver Gugger 2024-10-15 09:58:39 +02:00
parent 5d60da54a3
commit 8c79940374
No known key found for this signature in database
GPG Key ID: 8E4256593F177720
2 changed files with 10 additions and 20 deletions

View File

@ -54,10 +54,10 @@ var (
// replaceCustomData replaces the custom channel data hex string with the // replaceCustomData replaces the custom channel data hex string with the
// decoded custom channel data in the JSON response. // decoded custom channel data in the JSON response.
func replaceCustomData(jsonBytes []byte) ([]byte, error) { func replaceCustomData(jsonBytes []byte) []byte {
// If there's nothing to replace, return the original JSON. // If there's nothing to replace, return the original JSON.
if !customDataPattern.Match(jsonBytes) { if !customDataPattern.Match(jsonBytes) {
return jsonBytes, nil return jsonBytes
} }
replacedBytes := customDataPattern.ReplaceAllFunc( replacedBytes := customDataPattern.ReplaceAllFunc(
@ -78,10 +78,12 @@ func replaceCustomData(jsonBytes []byte) ([]byte, error) {
var buf bytes.Buffer var buf bytes.Buffer
err := json.Indent(&buf, replacedBytes, "", " ") err := json.Indent(&buf, replacedBytes, "", " ")
if err != nil { if err != nil {
return nil, err // If we can't indent the JSON, it likely means the replacement
// data wasn't correct, so we return the original JSON.
return jsonBytes
} }
return buf.Bytes(), nil return buf.Bytes()
} }
func getContext() context.Context { func getContext() context.Context {
@ -118,11 +120,7 @@ func printRespJSON(resp proto.Message) {
return return
} }
jsonBytesReplaced, err := replaceCustomData(jsonBytes) jsonBytesReplaced := replaceCustomData(jsonBytes)
if err != nil {
fmt.Println("unable to replace custom data: ", err)
jsonBytesReplaced = jsonBytes
}
fmt.Printf("%s\n", jsonBytesReplaced) fmt.Printf("%s\n", jsonBytesReplaced)
} }

View File

@ -131,7 +131,6 @@ func TestReplaceCustomData(t *testing.T) {
data string data string
replaceData string replaceData string
expected string expected string
expectedErr string
}{ }{
{ {
name: "no replacement necessary", name: "no replacement necessary",
@ -175,7 +174,8 @@ func TestReplaceCustomData(t *testing.T) {
name: "invalid json", name: "invalid json",
data: "this ain't json, " + data: "this ain't json, " +
"\"custom_channel_data\":\"a\"", "\"custom_channel_data\":\"a\"",
expectedErr: "invalid character 'h' in literal true", expected: "this ain't json, " +
"\"custom_channel_data\":\"a\"",
}, },
{ {
name: "valid json, invalid hex, just formatted", name: "valid json, invalid hex, just formatted",
@ -186,15 +186,7 @@ func TestReplaceCustomData(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
result, err := replaceCustomData([]byte(tc.data)) result := replaceCustomData([]byte(tc.data))
if tc.expectedErr != "" {
require.ErrorContains(t, err, tc.expectedErr)
return
}
require.NoError(t, err)
require.Equal(t, tc.expected, string(result)) require.Equal(t, tc.expected, string(result))
}) })
} }