hyperledger/fabric · error
too few arguments
Error message
too few arguments
What it means
In Hyperledger Fabric's statecouchdb package, GetDatabaseSecurity() fetches a database's _security document from CouchDB and decodes it into a databaseSecurity struct. If the raw HTTP response body is not valid JSON or does not match the expected struct (e.g. non-string members where []string are expected), json.Unmarshal fails and the error is wrapped as 'error unmarshalling json data'. This aborts the security-read operation.
Source
Thrown at ccaas_builder/cmd/detect/main.go:39
func main() {
logger.Println("::Detect")
if err := run(); err != nil {
logger.Printf("::Error: %v\n", err)
os.Exit(1)
}
logger.Printf("::Type detected as ccaas")
}
type chaincodeMetadata struct {
Type string `json:"type"`
}
func run() error {
if len(os.Args) < 3 {
return errors.New("too few arguments")
}
chaincodeMetaData := os.Args[2]
// Check metadata file's existence
metadataFile := filepath.Join(chaincodeMetaData, "metadata.json")
if _, err := os.Stat(metadataFile); err != nil {
return errors.WithMessagef(err, "%s not found ", metadataFile)
}
// Read the metadata file
mdbytes, err := os.ReadFile(metadataFile)
if err != nil {
return errors.WithMessagef(err, "%s not readable", metadataFile)
}
var metadata chaincodeMetadata
err = json.Unmarshal(mdbytes, &metadata)View on GitHub (pinned to 2736b63f8f)
Solutions
- Verify CouchDB is reachable directly and the URL/credentials are correct so it returns real JSON, not a proxy error page
- curl the _security endpoint (GET http://<couchdb>:5984/<db>/_security) and confirm the body is valid JSON matching {admins:{names:[],roles:[]},members:{...}}
- Check for a proxy/middleware rewriting the response; bypass it for the CouchDB host
- Check Fabric and CouchDB version compatibility
- Enable debug logging (couchdbLogger) to inspect the raw response
Example fix
// before
resp, err := http.Get("http://proxy:80/couchdb/db/_security") // proxy returns HTML
json.Unmarshal(raw, jsonResponse) // fails
// after
client := &http.Client{} // point directly at CouchDB, or check Content-Type first
resp, err := client.Get("http://couchdb:5984/db/_security")
if ct := resp.Header.Get("Content-Type"); !strings.HasPrefix(ct, "application/json") {
return nil, errors.New("non-JSON response from CouchDB")
} Defensive patterns
Strategy: type-guard
Validate before calling
raw, _ := io.ReadAll(resp.Body)
if !json.Valid(raw) {
return fmt.Errorf("non-JSON _security response: %.200s", raw)
} Type guard
func isJSON(b []byte) bool {
var v any
return json.Unmarshal(b, &v) == nil
} Try / catch
sec, err := db.GetDatabaseSecurity()
if err != nil {
if strings.Contains(err.Error(), "error unmarshalling json data") {
// log raw response, check CouchDB reachability/auth, retry
return fmt.Errorf("couchdb returned non-JSON security doc: %w", err)
}
return err
} Prevention
- Point the peer directly at CouchDB, not through a rewriting proxy
- Verify credentials so CouchDB returns JSON, not HTML error pages
- Periodically curl _security to confirm the response shape
- Pin compatible Fabric/CouchDB versions
- Watch peer logs for repeated unmarshal failures on the same DB
When it happens
Trigger: Calling GetDatabaseSecurity() when the CouchDB server returns a body that is not valid JSON (e.g. an HTML error page from a proxy, or a malformed/truncated _security response), or JSON whose shape does not fit databaseSecurity (admins/members names/roles fields of unexpected types).
Common situations: A reverse proxy or load balancer intercepting CouchDB traffic and returning HTML; CouchDB returning an error body for an unauthorized request that the client still tries to decode; corrupted or non-JSON response due to network interruption.
Understand the failure class
Background: "failed to unmarshal" / json.Unmarshal errors: why parsing a response into a Go struct fails and how to fix it — this error's family across 23 libraries.
Related errors
- Failed opening file %s: %v
- config isn't valid
- incorrect number of arguments
- connection.json not found in source folder: %s
- failed to marshal config
AI-assisted analysis of hyperledger/fabric@2736b63f8f (2026-09-04).
Data as JSON: /api/errors/26b0bbdb81bbff45.
Report an issue: GitHub.