{"record":{"id":"2fcff3d651e90a96","repo":"micro/go-micro","slug":"failed-to-get-object-from-bucket","errorCode":null,"errorMessage":"Failed to get object from bucket","messagePattern":"Failed to get object from bucket","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"store/nats-js-kv/nats.go","lineNumber":397,"sourceCode":"\n\t\tstore, err = n.js.CreateKeyValue(kv)\n\t\tif err != nil {\n\t\t\treturn nil, errors.Wrapf(err, \"Failed to create bucket (%s)\", kv.Bucket)\n\t\t}\n\t}\n\n\tn.buckets.Set(kv.Bucket, store)\n\n\treturn store, nil\n}\n\n// getRecord returns the record with the given key from the nats kv store.\nfunc (n *natsStore) getRecord(bucket nats.KeyValue, key string) (*store.Record, bool, error) {\n\tobj, err := bucket.Get(key)\n\tif errors.Is(err, nats.ErrKeyNotFound) {\n\t\treturn nil, false, store.ErrNotFound\n\t} else if err != nil {\n\t\treturn nil, false, errors.Wrap(err, \"Failed to get object from bucket\")\n\t}\n\n\tvar kv KeyValueEnvelope\n\tif err := json.Unmarshal(obj.Value(), &kv); err != nil {\n\t\treturn nil, false, errors.Wrap(err, \"Failed to unmarshal object\")\n\t}\n\n\tif obj.Operation() != nats.KeyValuePut {\n\t\treturn nil, false, nil\n\t}\n\n\treturn &store.Record{\n\t\tKey:      kv.Key,\n\t\tValue:    kv.Data,\n\t\tMetadata: kv.Metadata,\n\t}, true, nil\n}\n","sourceCodeStart":379,"sourceCodeEnd":415,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/store/nats-js-kv/nats.go#L379-L415","documentation":"Returned by getRecord (called from Read) when bucket.Get(key) fails with anything other than nats.ErrKeyNotFound (which is translated to store.ErrNotFound). The key lookup against the JetStream KV bucket failed at the transport or stream level.","triggerScenarios":"Calling store.Read(key) when the connection is broken, the stream has no leader, the request times out, or the key contains invalid characters producing a malformed get subject.","commonSituations":"Reading immediately after a NATS server restart with a stale connection; reading during cluster failover; keys built from untrusted input containing spaces or wildcards; JetStream request timeouts too aggressive.","solutions":["Inspect the wrapped error; if it's a connection/timeout problem, reinitialize the store and retry Read","Verify the bucket stream is healthy and has a leader ('nats stream info KV_<bucket>')","Sanitize keys to valid NATS key characters before writing/reading","Increase JetStream request timeout via nats.JSOpt if timeouts are the cause"],"exampleFix":"// before\nkey := rawUserInput // may contain spaces\nrecs, err := st.Read(key)\n// after\nkey := url.PathEscape(strings.TrimSpace(rawUserInput))\nrecs, err := st.Read(key)","handlingStrategy":"retry","validationCode":"if key == \"\" || strings.ContainsAny(key, \" *>\") {\n    return fmt.Errorf(\"invalid key %q\", key)\n}","typeGuard":null,"tryCatchPattern":"recs, err := st.Read(key)\nif err != nil {\n    if errors.Is(err, store.ErrNotFound) {\n        return nil, err // genuine miss, don't retry\n    }\n    if strings.Contains(err.Error(), \"Failed to get object from bucket\") {\n        time.Sleep(backoff)\n        recs, err = st.Read(key)\n    }\n}","preventionTips":["Distinguish store.ErrNotFound (do not retry) from transport errors (retry)","Sanitize keys built from user input","Reconnect on NATS disconnect events before retrying reads","Set realistic JetStream request timeouts for your network"],"tags":["nats","jetstream","read","kv"],"backgroundTag":"kv-get-failed","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}