{"record":{"id":"22c1abad0f8700a6","repo":"hyperledger/fabric","slug":"no-list-open-cannot-add-entries","errorCode":null,"errorMessage":"no list open, cannot add entries","messagePattern":"no list open, cannot add entries","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/ledgerutil/jsonrw/json_read_write.go","lineNumber":179,"sourceCode":"func (w *JSONFileWriter) CloseList() error {\n\tif !w.listOpened {\n\t\treturn errors.Errorf(\"no list open, cannot close list\")\n\t}\n\n\tw.listOpened = false\n\t_, err := w.buffer.Write([]byte(\"]\\n\"))\n\tif err != nil {\n\t\treturn err\n\t}\n\n\treturn nil\n}\n\n// Add entries to an open json list\nfunc (w *JSONFileWriter) AddEntry(r any) error {\n\t// Need to open list before adding entries\n\tif !w.listOpened {\n\t\treturn errors.Errorf(\"no list open, cannot add entries\")\n\t}\n\t// Add commas for entries after the first entry in the list\n\tif w.firstEntryWritten {\n\t\t_, err := w.buffer.Write([]byte(\",\\n\"))\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t} else {\n\t\tw.firstEntryWritten = true\n\t}\n\n\terr := w.encoder.Encode(r)\n\tif err != nil {\n\t\treturn err\n\t}\n\tw.count++\n\n\treturn nil","sourceCodeStart":161,"sourceCodeEnd":197,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/internal/ledgerutil/jsonrw/json_read_write.go#L161-L197","documentation":"API-misuse guard on JSONFileWriter.AddEntry: entries can only be appended to a list previously opened with OpenList; the call arrived while listOpened is false, so the entry would produce invalid JSON and is rejected.","triggerScenarios":"Calling AddEntry before OpenList or after CloseList has closed the current list.","commonSituations":"Writing entries at the file's top level without wrapping them in a list, or continuing to add entries in a loop after the list was closed.","solutions":["Wrap AddEntry calls between OpenList and CloseList","Move AddEntry inside the list-open region","For top-level arrays, open one list for the whole file before adding entries"],"exampleFix":"// before\nw.AddEntry(\"a\") // errors: no list open\n// after\nw.OpenList()\nw.AddEntry(\"a\")\nw.CloseList()","handlingStrategy":"validation","validationCode":"func requireListOpen(listOpened bool) error {\n\tif !listOpened { return errors.New(\"AddEntry requires an open list: call OpenList first\") }\n\treturn nil\n}","typeGuard":"func canAddEntry(listOpened bool) bool { return listOpened }","tryCatchPattern":"if err := w.OpenList(); err != nil { return err }\nif err := w.AddEntry(item); err != nil { return err }\nif err := w.CloseList(); err != nil { return err }","preventionTips":["Open the list before the entry loop, close it after","Never write entries at the file's top level without a wrapping list","Check AddEntry errors so a failed write doesn't desync state"],"tags":["hyperledger-fabric","json","api-misuse"],"backgroundTag":"json-writer-no-list-open","analyzedSha":"2736b63f8fd5932511d56fe68b7039d15977f7f6","analyzedAt":"2026-09-04T08:52:36.465Z","contentChangedAt":"2026-09-04T08:52:36.465Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}