Skip to content

Commit 5196d40

Browse files
committed
fix: Fall back to JSON when response is not table-friendly
Some API responses (e.g. ask-ava-sync) return plain strings or other non-tabular data that caused table rendering to error out. Now gracefully falls back to indented JSON output instead of failing.
1 parent 32b3068 commit 5196d40

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

main.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,12 @@ func (t dciTableContentType) Marshal(value interface{}) ([]byte, error) {
881881

882882
rows, err := toTableRows(jsonSafe)
883883
if err != nil {
884-
return nil, err
884+
// Response is not table-friendly; fall back to indented JSON.
885+
b, jsonErr := json.MarshalIndent(jsonSafe, "", " ")
886+
if jsonErr != nil {
887+
return nil, err // return original table error
888+
}
889+
return append(b, '\n'), nil
885890
}
886891
return renderTable(rows)
887892
}

main_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,3 +1013,41 @@ func TestTableOverheadFormula(t *testing.T) {
10131013
}
10141014
}
10151015
}
1016+
1017+
func TestTableMarshalFallsBackToJSON(t *testing.T) {
1018+
ct := dciTableContentType{}
1019+
1020+
tests := []struct {
1021+
name string
1022+
input interface{}
1023+
}{
1024+
{"plain string", "How are you?"},
1025+
{"number", 42},
1026+
{"bool", true},
1027+
{"array of strings", []interface{}{"hello", "world"}},
1028+
}
1029+
1030+
for _, tc := range tests {
1031+
t.Run(tc.name, func(t *testing.T) {
1032+
out, err := ct.Marshal(tc.input)
1033+
if err != nil {
1034+
t.Fatalf("expected JSON fallback, got error: %v", err)
1035+
}
1036+
if len(out) == 0 {
1037+
t.Fatal("expected non-empty output")
1038+
}
1039+
})
1040+
}
1041+
}
1042+
1043+
func TestTableMarshalObjectStillWorks(t *testing.T) {
1044+
ct := dciTableContentType{}
1045+
input := map[string]interface{}{"name": "test", "value": 123}
1046+
out, err := ct.Marshal(input)
1047+
if err != nil {
1048+
t.Fatalf("expected table output, got error: %v", err)
1049+
}
1050+
if len(out) == 0 {
1051+
t.Fatal("expected non-empty output")
1052+
}
1053+
}

0 commit comments

Comments
 (0)