{"record":{"id":"764e412951d247b7","repo":"micro/go-micro","slug":"failed-to-store-data-in-bucket-s","errorCode":null,"errorMessage":"Failed to store data in bucket '%s'","messagePattern":"Failed to store data in bucket '(.+?)'","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"store/nats-js-kv/nats.go","lineNumber":242,"sourceCode":"\t\topt.Table = n.opts.Table\n\t}\n\n\tstore, err := n.mustGetBucketByName(opt.Database)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tb, err := json.Marshal(KeyValueEnvelope{\n\t\tKey:      rec.Key,\n\t\tData:     rec.Value,\n\t\tMetadata: rec.Metadata,\n\t})\n\tif err != nil {\n\t\treturn errors.Wrap(err, \"Failed to marshal object\")\n\t}\n\n\tif _, err := store.Put(n.NatsKey(opt.Table, rec.Key), b); err != nil {\n\t\treturn errors.Wrapf(err, \"Failed to store data in bucket '%s'\", n.NatsKey(opt.Table, rec.Key))\n\t}\n\n\treturn nil\n}\n\n// Delete removes the record with the corresponding key from the store.\nfunc (n *natsStore) Delete(key string, opts ...store.DeleteOption) error {\n\tif err := n.initConn(); err != nil {\n\t\treturn err\n\t}\n\n\topt := store.DeleteOptions{}\n\n\tfor _, o := range opts {\n\t\to(&opt)\n\t}\n\n\tif opt.Database == \"\" {","sourceCodeStart":224,"sourceCodeEnd":260,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/store/nats-js-kv/nats.go#L224-L260","documentation":"Returned by Write when the NATS KV store rejects the Put of the marshaled envelope, wrapped with the key that failed. This means the JetStream bucket could not accept the write, most often due to a connection problem or a key name the bucket will not accept.","triggerScenarios":"Calling store.Write(rec) where the NATS connection is down/stale, the server has lost quorum for the stream, or the resulting key (n.NatsKey(opt.Table, rec.Key)) is empty or contains characters/sequences invalid for NATS subjects (e.g. spaces, trailing dots).","commonSituations":"NATS server restarted or network blip while the client keeps the old conn; writing a key derived from user input containing spaces or wildcard/invalid characters; bucket stream under low-memory/disk pressure causing publishes to fail; per-message TTL options incompatible with server version.","solutions":["Check the wrapped error for a NATS connection issue; if the server was restarted, recreate/reinit the store so a fresh connection is established","Validate/sanitize rec.Key and opt.Table to contain only valid NATS key characters (alphanumerics, '.', '-', '_', '/', no spaces, no trailing '.')","Verify the JetStream stream for the bucket is healthy ('nats stream info KV_<bucket>') and that the server has jetstream resources available","Retry the Write with backoff if the wrapped error is transient (nats: timeout / no responders)"],"exampleFix":"// before\nkey := fmt.Sprintf(\"user %s\", email)\nstore.Write(&store.Record{Key: key, Value: data})\n// after\nkey := strings.ReplaceAll(email, \" \", \"_\")\nstore.Write(&store.Record{Key: key, Value: data})","handlingStrategy":"retry","validationCode":"var validKey = regexp.MustCompile(`^[a-zA-Z0-9._/-]+$`)\nif !validKey.MatchString(rec.Key) {\n    return fmt.Errorf(\"invalid key: %q\", rec.Key)\n}","typeGuard":null,"tryCatchPattern":"if err := st.Write(rec); err != nil {\n    if strings.Contains(err.Error(), \"Failed to store data in bucket\") {\n        time.Sleep(backoff)\n        err = st.Write(rec)\n    }\n    return err\n}","preventionTips":["Sanitize keys (no spaces, wildcards, or trailing dots) before Write","Monitor NATS connection state (nc.Status / disconnect callbacks) and reconnect on drop","Retry transient publish errors with exponential backoff","Watch JetStream stream health and account limits for the bucket"],"tags":["nats","jetstream","write","kv"],"backgroundTag":"kv-put-failed","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}