{"record":{"id":"1a1151eec3107561","repo":"kopia/kopia","slug":"invalid-duration-s-w","errorCode":null,"errorMessage":"invalid duration %s: %w","messagePattern":"invalid duration (.+?): %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"repo/jsonencoding/jsonencoding.go","lineNumber":34,"sourceCode":"// MarshalText writes d as text.\nfunc (d Duration) MarshalText() ([]byte, error) {\n\treturn []byte(d.String()), nil\n}\n\n// UnmarshalText read d from a text representation.\nfunc (d *Duration) UnmarshalText(b []byte) error {\n\ts := string(bytes.TrimSpace(b))\n\n\tf, err := strconv.ParseFloat(s, 64)\n\tif err == nil {\n\t\td.Duration = time.Duration(f)\n\n\t\treturn nil\n\t}\n\n\td.Duration, err = time.ParseDuration(s)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"invalid duration %s: %w\", s, err)\n\t}\n\n\treturn nil\n}\n","sourceCodeStart":16,"sourceCodeEnd":39,"githubUrl":"https://github.com/kopia/kopia/blob/82495e54b584c1ef6073c9e1be048f57f8aef078/repo/jsonencoding/jsonencoding.go#L16-L39","documentation":"jsonencoding.Duration.UnmarshalText (jsonencoding.go:34) first tries to interpret the text as a plain float of nanoseconds; if that fails it falls back to time.ParseDuration. When both fail, the raw value is wrapped as \"invalid duration %s\". Any JSON/text field using this Duration type must contain a Go-style duration string or a number of nanoseconds.","triggerScenarios":"Unmarshaling JSON/YAML into jsonencoding.Duration with values like \"1 hour\", \"1h30m\", \"5 mins\", \"daily\", or \"\" — strings time.ParseDuration cannot parse (no unit, unsupported unit, embedded spaces).","commonSituations":"Hand-edited config files writing human-friendly durations (\"30 days\", \"1 week\"); cron-like strings; locale-formatted numbers with thousand separators; empty values.","solutions":["Use Go duration syntax with units: \"300ms\", \"1h30m\", \"24h\", \"720h\" for 30 days.","Or provide a plain number interpreted as nanoseconds (e.g. 5000000).","Convert unsupported units manually (weeks→168h, days→24h) in the config.","Pre-validate the string with time.ParseDuration before writing it into the config."],"exampleFix":"// before\n{\"interval\": \"1 day\"}\n// after\n{\"interval\": \"24h\"}","handlingStrategy":"validation","validationCode":"func parseDur(s string) error {\n    if _, err := time.ParseDuration(s); err != nil {\n        if _, ferr := strconv.ParseFloat(s, 64); ferr != nil {\n            return fmt.Errorf(\"invalid duration %q\", s)\n        }\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"if err := json.Unmarshal(data, &cfg); err != nil {\n    var durErr error\n    if strings.Contains(err.Error(), \"invalid duration\") {\n        return fmt.Errorf(\"check duration fields (use Go syntax like 24h): %w\", err)\n    }\n    return err\n}","preventionTips":["Write durations in Go syntax with units: 30s, 24h, 720h.","Never use spaces or words like days/weeks in duration fields.","Validate duration strings with time.ParseDuration before writing configs."],"tags":["json","duration","parsing"],"backgroundTag":"invalid-duration-format","analyzedSha":"82495e54b584c1ef6073c9e1be048f57f8aef078","analyzedAt":"2026-09-07T20:35:21.689Z","contentChangedAt":"2026-09-07T20:35:21.689Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}