{"record":{"id":"7600f65bb839bc20","repo":"nats-io/nats-server","slug":"can-not-marshal-v","errorCode":null,"errorMessage":"can not marshal %v","messagePattern":"can not marshal (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/store.go","lineNumber":581,"sourceCode":"\tcase InterestPolicy:\n\t\treturn \"Interest\"\n\tcase WorkQueuePolicy:\n\t\treturn \"WorkQueue\"\n\tdefault:\n\t\treturn \"Unknown Retention Policy\"\n\t}\n}\n\nfunc (rp RetentionPolicy) MarshalJSON() ([]byte, error) {\n\tswitch rp {\n\tcase LimitsPolicy:\n\t\treturn limitsPolicyJSONBytes, nil\n\tcase InterestPolicy:\n\t\treturn interestPolicyJSONBytes, nil\n\tcase WorkQueuePolicy:\n\t\treturn workQueuePolicyJSONBytes, nil\n\tdefault:\n\t\treturn nil, fmt.Errorf(\"can not marshal %v\", rp)\n\t}\n}\n\nfunc (rp *RetentionPolicy) UnmarshalJSON(data []byte) error {\n\tswitch string(data) {\n\tcase limitsPolicyJSONString:\n\t\t*rp = LimitsPolicy\n\tcase interestPolicyJSONString:\n\t\t*rp = InterestPolicy\n\tcase workQueuePolicyJSONString:\n\t\t*rp = WorkQueuePolicy\n\tdefault:\n\t\treturn fmt.Errorf(\"can not unmarshal %q\", data)\n\t}\n\treturn nil\n}\n\nfunc (dp DiscardPolicy) String() string {","sourceCodeStart":563,"sourceCodeEnd":599,"githubUrl":"https://github.com/nats-io/nats-server/blob/3a66a489d262bf89b71a71c955c94920394532f3/server/store.go#L563-L599","documentation":"RetentionPolicy.MarshalJSON in server/store.go only supports the defined enum values (LimitsPolicy, InterestPolicy, WorkQueuePolicy). If a RetentionPolicy variable holds an out-of-range value (e.g. zero value before assignment, or a value from an incompatible version), MarshalJSON hits the default branch and returns this error, which json.Marshal surfaces as an error wrapping \"can not marshal %v\".","triggerScenarios":"Calling json.Marshal on a Channel or ChannelConfig whose RetentionPolicy field was never set to one of the three defined constants (e.g. declared as var rp RetentionPolicy leaving 0 invalid, or deserialized from a file with an unknown value).","commonSituations":"Persisting NATS Streaming channel configuration to disk with a corrupted/zeroed field; upgrading or downgrading nats-streaming-server between versions where the policy enum differs; hand-editing a config file with an invalid policy string that was silently mapped to an invalid numeric value.","solutions":["Set the RetentionPolicy explicitly to LimitsPolicy, InterestPolicy, or WorkQueuePolicy before marshaling.","Validate the policy value against the defined constants before calling json.Marshal.","Check the source of the value (config file, network payload) for corruption or version mismatch and re-create the config with a current version of the library."],"exampleFix":"// before\nvar rp stan.RetentionPolicy\nb, err := json.Marshal(rp) // can not marshal 0\n// after\nrp := stan.LimitsPolicy\nb, err := json.Marshal(rp) // ok","handlingStrategy":"validation","validationCode":"func validRetentionPolicy(rp stan.RetentionPolicy) bool {\n\treturn rp == stan.LimitsPolicy || rp == stan.InterestPolicy || rp == stan.WorkQueuePolicy\n}","typeGuard":"func isRetentionPolicy(v int) (stan.RetentionPolicy, bool) {\n\trp := stan.RetentionPolicy(v)\n\tswitch rp {\n\tcase stan.LimitsPolicy, stan.InterestPolicy, stan.WorkQueuePolicy:\n\t\treturn rp, true\n\t}\n\treturn 0, false\n}","tryCatchPattern":"b, err := json.Marshal(cfg)\nif err != nil {\n\tif strings.Contains(err.Error(), \"can not marshal\") {\n\t\t// fix or reject the policy value before retrying\n\t}\n\treturn err\n}","preventionTips":["Always initialize policy fields with named constants, never with var/zero values.","Validate channel configs before persisting or marshaling.","Use the same nats-streaming-server version for writing and reading state files."],"tags":["go","json","serialization","nats-streaming"],"backgroundTag":"json-marshal-unsupported-value","analyzedSha":"3a66a489d262bf89b71a71c955c94920394532f3","analyzedAt":"2026-09-02T04:41:54.247Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}